感觉是类似分治的思想,每一次向下都是第奇数个小球往左走,第偶数个小球往右走。然后接着往下向左走的小球有I=(I+1)/2;向右走的小球有I=I/2。那么直到要走d-1次,输出k。
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <map> 5 #include <set> 6 #include <algorithm> 7 #include <fstream> 8 #include <cstdio> 9 #include <cmath> 10 #include <stack> 11 #include <queue> 12 #include <deque> 13 using namespace std; 14 const double Pi=3.14159265358979323846; 15 typedef long long ll; 16 const int MAXN=(1<<20)+5; 17 const int dx[5]={0,0,0,1,-1}; 18 const int dy[5]={1,-1,0,0,0}; 19 const int INF = 0x3f3f3f3f; 20 const int NINF = 0xc0c0c0c0; 21 const ll mod=1e9+7; 22 bool s[MAXN]; 23 int main() 24 { 25 int D;int I; 26 int t;cin>>t;int k; 27 while(t--) 28 { 29 scanf("%d%d",&D,&I); 30 int n=(1<<D)-1; 31 k=1; 32 for(int i=1;i<=D-1;i++) 33 { 34 if(I%2) k=k*2; 35 else k=k*2+1; 36 I=(I+1)/2; 37 } 38 cout << k <<endl; 39 } 40 return 0; 41 }
原文:https://www.cnblogs.com/Msmw/p/10617836.html