题意很简单不说了。感觉就是一个找规律的题目。
可以发现每一行都是一个等差数列,公差从上往下递减,为2^k,而且首项也是2的幂次,找到现在在的数能被2的几次方整除即可。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXN = 100000 +10;
long long n;
int tot=1;
char a[MAXN];
long long b[100];
long long s[100];
int main(){
scanf("%lld",&n);
b[0]=1;
for(int i=1;i<=63;i++){
b[i]=b[i-1]*2;
}
int q;
scanf("%d",&q);
int tot=0;
for(tot=0;tot<=63;tot++){
if(b[tot]==(n/2+1)){
break;
}
}
while(q--){
long long u;
scanf("%I64d%s",&u,a);
for(int i=0;i<strlen(a);i++){
int x=0;
for(x=63;x>=0;x--){
if(u%b[x]==0){
break;
}
}
long long y=u/b[x];
y=(y+1)/2;
if(a[i]=='U'){
if(x!=tot){
long long temp=(y+1)/2;
temp=2*temp-1;
u=b[x+1]*temp;
}
}else if(a[i]=='L'){
if(x!=0){
long long temp=2*y-1;
temp=2*temp-1;
u=b[x-1]*temp;
}
}else{
if(x!=0){
long long temp=2*y;
temp=2*temp-1;
u=b[x-1]*temp;
}
}
}
printf("%I64d\n",u);
}
}