#define _CRT_NONSTDC_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #define ENABLE_DUMP //#define ENABLE_PERF #include #include #include #include #include //#include //#include //#include //using u128 = boost::multiprecision::uint128_t; //using i128 = boost::multiprecision::int128_t; #ifdef _MSC_VER #include #include #include //#include //#include //#include #include int __builtin_clz(unsigned int n) { unsigned long index; _BitScanReverse(&index, n); return 31 - index; } int __builtin_ctz(unsigned int n) { unsigned long index; _BitScanForward(&index, n); return index; } namespace std { inline int __lg(int __n) { return sizeof(int) * 8 - 1 - __builtin_clz(__n); } } #define __builtin_popcnt = __popcnt; #define __builtin_popcntll = __popcnt64; #else #pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #endif /** compro_io **/ #ifdef ATCODER_MODINT_HPP using mint = atcoder::modint998244353; std::ostream& operator<<(std::ostream& os, const mint& m) { os << m.val(); return os; } std::istream& operator>>(std::istream& is, mint& m) { uint64_t x; is >> x; m = x; return is; } #endif namespace aux { template struct tp { static void output(std::ostream& os, const T& v) { os << std::get(v) << ", "; tp::output(os, v); } }; template struct tp { static void output(std::ostream& os, const T& v) { os << std::get(v); } }; } template std::ostream& operator<<(std::ostream& os, const std::tuple& t) { os << '['; aux::tp, 0, sizeof...(Ts) - 1>::output(os, t); return os << ']'; } // tuple out template std::basic_ostream& operator<<(std::basic_ostream& os, const Container& x); // container out (fwd decl) template std::ostream& operator<<(std::ostream& os, const std::pair& p) { return os << "[" << p.first << ", " << p.second << "]"; } // pair out template std::istream& operator>>(std::istream& is, std::pair& p) { return is >> p.first >> p.second; } // pair in std::ostream& operator<<(std::ostream& os, const std::vector::reference& v) { os << (v ? '1' : '0'); return os; } // bool (vector) out std::ostream& operator<<(std::ostream& os, const std::vector& v) { bool f = true; os << "["; for (const auto& x : v) { os << (f ? "" : ", ") << x; f = false; } os << "]"; return os; } // vector out template std::basic_ostream& operator<<(std::basic_ostream& os, const Container& x) { bool f = true; os << "["; for (auto& y : x) { os << (f ? "" : ", ") << y; f = false; } return os << "]"; } // container out template())), class = typename std::enable_if::value>::type> std::istream& operator>>(std::istream& is, T& a) { for (auto& x : a) is >> x; return is; } // container in template auto operator<<(std::ostream& out, const T& t) -> decltype(out << t.stringify()) { out << t.stringify(); return out; } // struct (has stringify() func) out /** io setup **/ struct IOSetup { IOSetup(bool f) { if (f) { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } std::cout << std::fixed << std::setprecision(15); } } iosetup(true); // set false when solving interective problems /** string formatter **/ template std::string format(const std::string& f, Ts... t) { size_t l = std::snprintf(nullptr, 0, f.c_str(), t...); std::vector b(l + 1); std::snprintf(&b[0], l + 1, f.c_str(), t...); return std::string(&b[0], &b[0] + l); } /** dump **/ #ifdef ENABLE_DUMP #define DUMPOUT std::cerr std::ostringstream DUMPBUF; #define dump(...) do{DUMPBUF<<" ";DUMPBUF<<#__VA_ARGS__<<" :[DUMP - "<<__LINE__<<":"< void dump_func(Head&& head, Tail&&... tail) { DUMPBUF << head; if (sizeof...(Tail) == 0) { DUMPBUF << " "; } else { DUMPBUF << ", "; } dump_func(std::move(tail)...); } #else #define DUMP(...) void(0); #endif /* timer */ class Timer { double t = 0, paused = 0, tmp; public: Timer() { reset(); } static double time() { #ifdef _MSC_VER return __rdtsc() / 2.3e9; #else unsigned long long a, d; __asm__ volatile("rdtsc" : "=a"(a), "=d"(d)); return (d << 32 | a) / 2.3e9; #endif } void reset() { t = time(); } void pause() { tmp = time(); } void restart() { paused += time() - tmp; } double elapsed_ms() const { return (time() - t - paused) * 1000.0; } } timer; /** perf counter **/ #if !defined(__PRETTY_FUNCTION__) && !defined(__GNUC__) #define __PRETTY_FUNCTION__ __FUNCSIG__ #endif struct PerfCounter { std::string name; Timer timer; PerfCounter(const std::string& name_) : name(name_) {} ~PerfCounter() { #ifdef ENABLE_PERF std::cerr << format("[PerfCounter] %6d ms <%s>.\n", (int)timer.elapsed_ms(), name.c_str()); #endif } }; /** rand **/ struct Xorshift { static constexpr uint32_t M = UINT_MAX; static constexpr double e = 1.0 / M; uint64_t x = 88172645463325252LL; Xorshift() {} Xorshift(uint64_t seed) { reseed(seed); } inline void reseed(uint64_t seed) { x = 0x498b3bc5 ^ seed; for (int i = 0; i < 20; i++) next_u64(); } inline uint64_t next_u64() { x ^= x << 7; return x ^= x >> 9; } inline uint64_t next_u64(uint64_t mod) { return next_u64() % mod; } inline uint64_t next_u64(uint64_t l, uint64_t r) { return l + next_u64(r - l + 1); } inline uint32_t next_u32() { return next_u64() >> 32; } inline uint32_t next_u32(uint32_t mod) { return ((uint64_t)next_u32() * mod) >> 32; } inline uint32_t next_u32(uint32_t l, uint32_t r) { return l + next_u32(r - l + 1); } inline double next_double() { return next_u32() * e; } inline double next_double(double c) { return next_double() * c; } inline double next_double(double l, double r) { return next_double(r - l) + l; } }; /** shuffle **/ template void shuffle_vector(std::vector& v, Xorshift& rnd) { int n = v.size(); for (int i = n - 1; i >= 1; i--) { int r = rnd.next_u32(i); std::swap(v[i], v[r]); } } /** split **/ std::vector split(std::string str, const std::string& delim) { for (char& c : str) if (delim.find(c) != std::string::npos) c = ' '; std::istringstream iss(str); std::vector parsed; std::string buf; while (iss >> buf) parsed.push_back(buf); return parsed; } /** misc **/ template inline void Fill(A(&array)[N], const T& val) { std::fill((T*)array, (T*)(array + N), val); } // fill array template auto make_vector(T x, int arg, Args ...args) { if constexpr (sizeof...(args) == 0)return std::vector(arg, x); else return std::vector(arg, make_vector(x, args...)); } template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } /** hash **/ namespace aux { template inline void hash(std::size_t& s, const T& v) { s ^= std::hash()(v) + 0x9e3779b9 + (s << 6) + (s >> 2); } } namespace std { template struct hash> { size_t operator()(const std::pair& s) const noexcept { size_t seed = 0; aux::hash(seed, s.first); aux::hash(seed, s.second); return seed; } }; } /** using **/ using ll = int64_t; using ull = uint64_t; using ld = double; //using ld = boost::multiprecision::cpp_bin_float_quad; using pii = std::pair; using pll = std::pair; template using PQ = std::priority_queue; template using MPQ = std::priority_queue, std::greater>; using namespace std; template void print_vector(const std::vector& v) { for (int i = 0; i < v.size(); i++) { std::cout << (i ? " " : "") << v[i]; } std::cout << '\n'; } #if 1 inline double get_temp(double stemp, double etemp, double t, double T) { return etemp + (stemp - etemp) * (T - t) / T; }; #else inline double get_temp(double stemp, double etemp, double t, double T) { return stemp * pow(etemp / stemp, t / T); }; #endif constexpr int N = 45, M = 50; constexpr int64_t T = 500000000000000000; std::array A, B; std::array us, vs, pus, pvs, bus, bvs; void initialize() { cin >> A[0]; // ignore N for (int i = 0; i < N; i++) { cin >> A[i] >> B[i]; } for (int i = 0; i < M; i++) { us[i] = 0; vs[i] = 1; } } void initialize(Xorshift& rnd) { for (int i = 0; i < N; i++) { A[i] = rnd.next_u64(100000000000000000, 1000000000000000000); B[i] = rnd.next_u64(100000000000000000, 1000000000000000000); } for (int i = 0; i < M; i++) { us[i] = 0; vs[i] = 1; } } double compute_score() { std::array CA(A), CB(B); for (int i = 0; i < M; i++) { int u = us[i], v = vs[i]; CA[u] = CA[v] = (CA[u] + CA[v]) / 2; CB[u] = CB[v] = (CB[u] + CB[v]) / 2; } auto V1 = abs(T - CA[0]), V2 = abs(T - CB[0]); return 2e6 - 1e5 * log10(std::max(V1, V2) + 1.0); } double change_all(Xorshift& rnd) { double pscore = compute_score(); pus = us; pvs = vs; for (int i = 0; i < M; i++) { us[i] = rnd.next_u32(N); do { vs[i] = rnd.next_u32(N); } while (us[i] == vs[i]); } double nscore = compute_score(); return nscore - pscore; } double change_one(Xorshift& rnd) { double pscore = compute_score(); pus = us; pvs = vs; int i = rnd.next_u32(M); us[i] = rnd.next_u32(N); do { vs[i] = rnd.next_u32(N); } while (us[i] == vs[i]); double nscore = compute_score(); return nscore - pscore; } void solve() { Timer timer; Xorshift rnd; #ifdef _MSC_VER initialize(rnd); #else initialize(); #endif double score = compute_score(); double best_score = -1.0; int loop = 0; double start_time = timer.elapsed_ms(), now_time, end_time = 900; while ((now_time = timer.elapsed_ms()) < end_time) { loop++; if (!(loop & 0x3FFFF)) { //dump(loop, score, best_score); } double diff = change_one(rnd); double temp = get_temp(100.0, 0.0, now_time - start_time, end_time - start_time); double prob = exp(diff / temp); if (prob < rnd.next_double()) { us = pus; vs = pvs; } else { score += diff; if (chmax(best_score, score)) { bus = us; bvs = vs; } //dump(score); } } dump(loop, score, best_score); us = bus; vs = bvs; cout << M << '\n'; for (int i = 0; i < M; i++) { cout << us[i] + 1 << ' ' << vs[i] + 1 << '\n'; } } int main() { int T = 1; //cin >> T; for (int t = 0; t < T; t++) { solve(); } return 0; }