EOJ Monthly 2018.9 解密信件 【递归】

版权声明:如需转载,记得标识出处 https://blog.csdn.net/godleaf/article/details/82904255

题目链接:https://acm.ecnu.edu.cn/contest/106/problem/B/

思路:

不断递归到底部然后再反转,反转表达式是根据互补的关系得出来的

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<sstream>
#include<vector>
#include<string>
#include<set>

using namespace std;
//using namespace __gnu_pbds;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define REP(i,n) for(int i=0;i<n;++i)

int read(){

    int r=0,f=1;char p=getchar();
    while(p>'9'||p<'0'){if(p=='-')f=-1;p=getchar();}
    while(p>='0'&&p<='9'){r=r*10+p-48;p=getchar();}return r*f;
}

//typedef tree<pair<long long,int>,null_type,less< pair<long long,int> >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
const int Maxn = 1e7+5;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
const int Mod = 1e9+7;
const double PI = acos(-1.0);

void solve (ll L, ll R, ll &cur) { // 不断递归到底部,在反转
    if(L >= R) return;
    ll k = (R-L+1)/2; // 长度
    if(cur <= L+k-1) solve (L,L+k-1,cur);  
    else solve (L+k,R,cur);
    cur = L+(R-cur); // 反转表达式,cur表示当前位置,注意题目问的是知道加密后求加密前的位置
}

int main (void)
{
    int T;
    ll n,x;
    cin >> T;
    while (T--) {
        cin >> n >> x;
        solve (1,n,x);
        cout << x << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/godleaf/article/details/82904255