sgu 175 Encoding

来源:互联网 发布:泰坦尼克号细节知乎 编辑:程序博客网 时间:2024/05/22 15:18

175. Encoding

time limit per test: 0.5 sec.
memory limit per test: 4096 KB
input: standard
output: standard



Let phi(W) is the result of encoding for algorithm:
1. If the length of W is 1 then phi(W) is W;
2. Let coded word is W = w1w2...wN and K = N / 2 (rounded down);
3. phi(W) = phi(wNwN-1...wK+1) + phi(wKwK-1...w1).
For example, phi('Ok') = 'kO', phi('abcd') = 'cdab'.
Your task is to find position of letter wq in encoded word phi(W).

Input
Given integers N, q (1 <= N <= 10^9; 1<= q <= N), where N is the length of word W.

Output
Write position of letter wq in encoded word phi(W).

Sample test(s)

Input
9 4

Output
8

这是。。。。?编程能力测试题?
简单的递归方程就解决了。

贴上代码:
#include<iostream>#include<cstring>#include<cstdio>#include<set>#include<algorithm>#include<vector>#include<cstdlib>#define inf 0xfffffff#define CLR(a,b) memset((a),(b),sizeof((a)))#define FOR(a,b) for(int a=1;a<=(b);(a)++)using namespace std;int const nMax = 1010;int const base = 10;typedef int LL;typedef pair<LL,LL> pij;//    std::ios::sync_with_stdio(false);int ans;void phi (int l,int n,int p){    if(n==1) {        ans=l;        return ;    }    int k=n/2;    if(p<=k) {        phi(n-k+l,k,k-p+1);    }else {        phi(l,n-k,n-p+1);    }    return ;}int n,p;int main(){    scanf("%d%d",&n,&p);    phi(1,n,p);    printf("%d\n",ans);    return 0;}



原创粉丝点击