hdu 5063——Operation the Sequence

来源:互联网 发布:如何在淘宝买bt种子 编辑:程序博客网 时间:2024/05/18 03:31

代码如下:

#include<iostream>#include<cstdio>#include<cstring>using namespace std;typedef long long ll;const int mod=1000000007;int operat[100005];int tot;int n,m;int fun1(int x)//第一个函数是把奇数放前面偶数放后面,这个相当于题目给的函数的反函数,得到了一个位置x,推出经过第一个函数之前x的位置{    int odd=n>>1;    if(n&1)odd++;    if(x<=odd)return 2*x-1;    else    {        x-=odd;        return 2*x;    }}int fun2(int x)//第二个函数的反函数{    return n+1-x;}ll ans(ll x){    int mul=0;    for(int i=tot-1;i>=0;--i)    {        if(operat[i]==1)        {            x=fun1(x);        }        else if(operat[i]==2)        {            x=fun2(x);        }        else        {            mul++;//乘方记下来,最后再乘        }    }    for(int i=1;i<=mul;++i)    {        x=x*x%mod;    }    return x;}int main(){//    freopen("data.txt","r",stdin);    int T;    scanf("%d",&T);    while(T--)    {        tot=0;        scanf("%d%d",&n,&m);        getchar();        for(int i=0;i<m;++i)        {            char op;            int d;            scanf("%c %d",&op,&d);            getchar();            if(op=='O')            {                operat[tot++]=d;            }            else            {                printf("%I64d\n",ans((ll)d));            }        }    }    return 0;}


0 0