Submission #5826386


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) x.begin(),x.end()
#define dmp(x) cerr<<#x<<":"<<x<<endl
typedef pair<int,int> P;
typedef pair<int,P> PP; 
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
const int MOD = 1e9+7;

signed main(){
  int n,q;
  cin>>n>>q;
  for(int i=0;i<q;i++){
    int v,w;
    cin>>v>>w;
    if(n==1){
	 cout<<min(v,w)<<endl;
	 continue;
    }
    vector<int> vv,wv;
    while(v>1){
	 vv.pb(v);
	 v=(v+n-2)/n;
    }
    while(w>1){
	 wv.pb(w);
	 w=(w+n-2)/n;
    }
    sort(all(vv));
    sort(all(wv));
    int ans=1;
    for(int j=0;j<sz(vv)&&j<sz(wv);j++){
	 if(vv[j]!=wv[j])break;
	 ans=vv[j];
    }
    cout<<ans<<endl;
  }
  return 0;
}

Submission Info

Submission Time
Task B - Evergrowing Tree
User shake
Language C++14 (GCC 5.4.1)
Score 100
Code Size 946 Byte
Status AC
Exec Time 459 ms
Memory 1280 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 20
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 266 ms 384 KB
b05 AC 245 ms 1280 KB
b06 AC 459 ms 384 KB
b07 AC 446 ms 384 KB
b08 AC 241 ms 1152 KB
b09 AC 440 ms 384 KB
b10 AC 415 ms 384 KB
b11 AC 403 ms 384 KB
b12 AC 377 ms 384 KB
b13 AC 364 ms 384 KB
b14 AC 297 ms 384 KB
b15 AC 287 ms 384 KB
b16 AC 280 ms 512 KB
b17 AC 279 ms 384 KB
b18 AC 287 ms 384 KB
b19 AC 272 ms 384 KB
b20 AC 260 ms 384 KB