sgu 175 分类: sgu 2015-03-08 16:35 53人阅读 评论(0) 收藏

递归处理[想象中的]字符串,因为只需要知道其中一个元素最后的位置,
可以只调用log2N次递归就得出答案


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
//phi(W) = phi(wNwN-1...wK+1) + phi(wKwK-1...w1). 
int f(const int ni,const int qi)
{
    if(ni == 1)return 1;

    int mid = ni>>1;

    if(qi <= mid)
    {
        return (ni - mid) + f(mid , mid - qi + 1);
    }
    else
    {
        return f((ni - mid),ni - qi + 1);
    }
}

int main()
{
    int n , q;

#ifndef ONLINE_JUDGE    
    freopen("sgu175.in","r",stdin);
    freopen("sgu175.out","w",stdout);
#endif

    scanf("%d%d",&n,&q);

    printf("%d\n",f(n , q));

#ifndef ONLINE_JUDGE
    fclose(stdin);  
    fclose(stdout);
#endif
    return 0;   
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-03-08 16:35  <Dash>  阅读(120)  评论(0)    收藏  举报