Operation the Sequence


Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 607    Accepted Submission(s): 227



Problem Description


a1=1,a2=2,a3=3,…,an=n. Then give you m operators, you should process all the operators in order. Each operator is one of four types: 
 
 Type1: O 1 call fun1(); 
 
 Type2: O 2 call fun2(); 
 
 Type3: O 3 call fun3(); 
 
 Type4: Q i query current value of a[i], 
 this operator will have at most 50. 
 
 Global Variables: a[1…n],b[1…n]; 
 
 fun1() { 
 
 index=1; 
 
   for(i=1; i<=n; i +=2) 
 
     b[index++]=a[i]; 
 
   for(i=2; i<=n; i +=2) 
 
     b[index++]=a[i]; 
 
   for(i=1; i<=n; ++i) 
 
     a[i]=b[i]; 
 
 } 
 
 fun2() { 
 
   L = 1;R = n; 
 
   while(L<R) { 
 
     Swap(a[L], a[R]); 
 
     ++L;--R; 
 
   } 
 
 } 
 
 fun3() { 
 
   for(i=1; i<=n; ++i) 
 
     a[i]=a[i]*a[i]; 
 
 }


 



Input


T(1≤T≤20), indicating the number of test cases. 
 
 The first line of each test case contains two integer 
   
 n(0<n≤100000), 
  
 m(0<m≤100000). 
 
 Then m lines follow, each line represent an operator above.


Output


For each test case, output the query values, the values may be so large, you just output the values mod 1000000007(1e9+7).


 



Sample Input


1
3 5
O 1
O 2
Q 1
O 3
Q 1

 



Sample Output


2 4


 



Source


BestCoder Round #13
题目大意:给出四种操作,第一种是将奇数序的数提前,偶数序的数滞后,第二种是前后对称互换位置,第三种是每一位求取平方

题目分析:因为查询次数少,所以只要在每次查询,根据逆推公式得到最初没经过变化的位置,然后根据记录的次数取平方(注意,不是求n次方,别理解错)
逆推公式就是简单的数学换算,对于操作1的要分奇偶和前后位置分类讨论,具体见代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define MOD 1000000007
#define MAX 200007

using namespace std;

typedef long long LL;
LL a[MAX];
int p,cnt1,cnt2;

int t,n,m;

int bf1 ( int i )
{
    if ( i > (n+1)/2 )
    {
        int temp = i*2-n-1;
        if ( n&1 ) return temp;
        else return temp+1;
    }
    else return i*2-1;
}

int bf2 ( int i )
{
    return n+1-i;
}

void f3 ( )
{
    p++;
}

LL ans[107];
char s[5];
int q[MAX],x;

int query ( int x , int cnt )
{
    for ( int i = cnt-1 ; i >= 0 ; i-- )
        if ( q[i] == 1 ) x = bf1(x);
        else x = bf2(x);
    return x;
}

int main ( )
{
    scanf ( "%d" , &t );
    while ( t-- )
    {
        
        p = cnt1 = cnt2 = 0;
        scanf ( "%d%d" , &n , &m );
        for ( int i = 1 ; i <= n ; i++ )
            a[i] = i;
        for ( int i = 1 ; i <= m ; i++ )
        {
            scanf ( "%s" , s );
            scanf ( "%d" , &x );
            if ( s[0] == 'O' )
            {
                if ( x == 3 ) f3( );
                else q[cnt1++] = x;
            }
            else 
            {
                int id = query ( x , cnt1 );
                LL tx = a[id];
                //cout << tx << endl;
                for ( int i = 0 ; i < p ; i++ )
                    tx *= tx , tx %= MOD;
                ans[cnt2++] = tx;
            }
        }
      for ( int i = 0 ; i < cnt2 ; i++ )
        printf ( "%I64d\n" , ans[i] );  
    }
}