Submission #1815989
Source Code Expand
#include <iostream> #include <iomanip> // << fixed << setprecision(xxx) #include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ; #include <vector> #include <string> // to_string(nnn) // substr(m, n) // stoi(nnn) #include <complex> #include <tuple> // get<n>(xxx) #include <queue> #include <stack> #include <map> // if (M.find(key) != M.end()) { } #include <set> // S.insert(M); // if (S.find(key) != S.end()) { } // for (auto it=S.begin(); it != S.end(); it++) { } // auto it = S.lower_bound(M); #include <random> // random_device rd; mt19937 mt(rd()); #include <cctype> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> // atoi(xxx) using namespace std; #define DEBUG 0 // change 0 -> 1 if we need debug. // insert #if<tab> by my emacs. #if DEBUG == 1 ... #end typedef long long ll; // const int dx[4] = {1, 0, -1, 0}; // const int dy[4] = {0, 1, 0, -1}; // const int C = 1e6+10; // const ll M = 1000000007; ll N, Q; vector<ll> waru(ll x) { vector<ll> ans; ans.push_back(x); while (x > 0) { x = (x-1)/N; ans.push_back(x); } reverse(ans.begin(), ans.end()); return ans; } ll solve(ll x, ll y) { if (N == 1) { return min(x, y); } vector<ll> V = waru(x); vector<ll> W = waru(y); int ind = 0; ll ans = 0; while (ind < (int)V.size() && ind < (int)W.size()) { if (V[ind] == W[ind]) { ans = V[ind]; ind++; } else { break; } } return ans; } int main () { cin >> N >> Q; for (auto i = 0; i < Q; ++i) { ll v, w; cin >> v >> w; v--; w--; cout << solve(v, w) + 1 << endl; } }
Submission Info
Submission Time | |
---|---|
Task | B - Evergrowing Tree |
User | kazunetakahashi |
Language | C++14 (GCC 5.4.1) |
Score | 100 |
Code Size | 1682 Byte |
Status | AC |
Exec Time | 430 ms |
Memory | 1280 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 100 / 100 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | a01, a02 |
All | a01, a02, b03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20 |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
a01 | AC | 1 ms | 256 KB |
a02 | AC | 1 ms | 256 KB |
b03 | AC | 1 ms | 256 KB |
b04 | AC | 275 ms | 384 KB |
b05 | AC | 248 ms | 1280 KB |
b06 | AC | 430 ms | 384 KB |
b07 | AC | 419 ms | 384 KB |
b08 | AC | 242 ms | 1152 KB |
b09 | AC | 428 ms | 384 KB |
b10 | AC | 392 ms | 384 KB |
b11 | AC | 366 ms | 384 KB |
b12 | AC | 363 ms | 384 KB |
b13 | AC | 345 ms | 384 KB |
b14 | AC | 296 ms | 384 KB |
b15 | AC | 290 ms | 384 KB |
b16 | AC | 283 ms | 384 KB |
b17 | AC | 285 ms | 384 KB |
b18 | AC | 283 ms | 384 KB |
b19 | AC | 283 ms | 384 KB |
b20 | AC | 265 ms | 384 KB |