您的位置:首页 > 产品设计 > UI/UE

hdu 5063 Operation the Sequence

2015-02-07 16:00 549 查看
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=1000000007;

int n,m;
char str[1000];
int a[100010];

ll pow_mod(ll a,ll p,ll n)
{
if(p==0) return 1;
ll ans=pow_mod(a,p/2,n);
ans=ans*ans%n;
if(p%2==1) ans=ans*a%n;
return ans;
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
getchar();
char op;
int cnt=0;
int pos=0;
int mid=(n+1)/2;
int x;
for(int i=1; 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=1;
for(int j=i-1; j>=1; j--)
{
if(a[j]==1)
{
if(pos<=mid)
pos=pos*2-1;
else
pos=(pos-mid)*2;
}
else if(a[j]==2)
{
pos=n-pos+1;
}
else if(a[j]==3)
{
c=(c+c)%(mod-1);
}
}
printf("%d\n",pow_mod(pos,c,mod));
}
getchar();
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: