Submission #1815972


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) {
  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 0
Code Size 1637 Byte
Status TLE
Exec Time 2107 ms
Memory 1049936 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 2
AC × 18
TLE × 2
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 270 ms 384 KB
b05 TLE 2107 ms 1049936 KB
b06 AC 416 ms 384 KB
b07 AC 414 ms 384 KB
b08 TLE 2105 ms 1049552 KB
b09 AC 421 ms 384 KB
b10 AC 391 ms 384 KB
b11 AC 370 ms 384 KB
b12 AC 350 ms 384 KB
b13 AC 344 ms 384 KB
b14 AC 296 ms 384 KB
b15 AC 281 ms 384 KB
b16 AC 284 ms 384 KB
b17 AC 279 ms 384 KB
b18 AC 277 ms 384 KB
b19 AC 280 ms 384 KB
b20 AC 268 ms 384 KB