20150205
http://codeforces.com/problemset/problem/507/C
代码:
#include <iostream>
using namespace std;
int main()
{
//Init 1
int num;
cin >> num;
//Init 2
long long s[51];
s[0] = 1;
for(int i = 1; i <= num; i++)
s[i] = s[i-1] * 2;
//Init 3
long long d;//目标数值
cin >> d;
int next[51];//0 means Left,1 means Right
//Run 1
for(int i = num - 1; i >= 0; i--){
if(d % 2 == 0){
next[i] = 1;
d = d / 2;
}else{
d = (d+1) / 2;
next[i] = 0;
}
}
//Run 2
d = 0;int last = 1;
for(int i = 0; i < num; i++){
if(next[i] == last)
d += s[num-i];
else
d += 1;
last = next[i];
}
//Result
cout << d << endl;
return 0;
}