Introduction

This is my blog of programming, I take notes and leave codes of computer science problems I solved here. Be my guest to comment :)

Sunday, October 28, 2012

uva 679 - Dropping Balls


Problem link
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
const int maxn = 1048577;
//-------------------------------
bool a[maxn];
int n;
//-------------------------------
int go(int x) {
 int r=1;
 while (x>0)
 {
  if (x%2!=0)
  {
   r*=2;
   x=(x+1)/2;
  }
  else
  {
   r=r*2+1;
   x=x/2;
  }
  if (r*2>n) break;
 }
 return(r);
}
//-------------------------------
int main() {
 int nTest;
 cin >> nTest;
 for (int test=1; test<=nTest; test++)
 {
  int d,l;
  cin >> d >> l;
  l = (l-1)%(1<<(d-1))+1;
  n=(1<<(d))-1;
  cout << go(l) << endl;
 }
 return 0;
}
//-------------------------------

No comments:

Post a Comment