hdu 5063 Operation the Sequence

时间:2023-03-09 19:09:10
hdu 5063 Operation the Sequence

http://acm.hdu.edu.cn/showproblem.php?pid=5063

思路:因为3查询最多50,所以可以在查询的时候逆操作找到原来的位置,然后再求查询的值。

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int mod=; int n,m;
char str[];
int a[]; ll pow_mod(ll a,ll p,ll n)
{
if(p==) return ;
ll ans=pow_mod(a,p/,n);
ans=ans*ans%n;
if(p%==) ans=ans*a%n;
return ans;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(a,,sizeof(a));
scanf("%d%d",&n,&m);
getchar();
char op;
int cnt=;
int pos=;
int mid=(n+)/;
int x;
for(int i=; i<=m; i++)
{
scanf("%c",&op);
if(op=='O')
{
scanf("%d",&x);
a[i]=x;
}
else if(op=='Q')
{
scanf("%d",&x);
pos=x;
int c=;
for(int j=i-; j>=; j--)
{
if(a[j]==)
{
if(pos<=mid)
pos=pos*-;
else
pos=(pos-mid)*;
}
else if(a[j]==)
{
pos=n-pos+;
}
else if(a[j]==)
{
c=(c+c)%(mod-);
}
}
printf("%d\n",pow_mod(pos,c,mod));
}
getchar();
}
}
return ;
}