Operation the Sequence
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5063
解题思路:
BestCoder官方题解:
注意到查询次数不超过50次,那么可以从查询位置逆回去操作,就可以发现它在最初序列的位置,再逆回去即可求得当前查询的
值,对于一组数据复杂度约为O(50*n)。
注意到题目的这句话,Type4: Q i query current value of a[i], this operator will have at most 50.然后就知道可以直接用暴力求解了。
AC代码:
#include <iostream>
#include <cstdio>
#define MOD 1000000007
using namespace std;
typedef long long ll;
int n,m,ans;
int op[100010];
int fun1(int x){ //题目第一个函数的反函数
int odd = n>>1;
if(n&1)
odd++;
if(x <= odd)
return 2*x-1;
else
return 2*(x-odd);
}
int fun2(int x){ //题目第二个函数的反函数
return n-(x-1);
}
ll solve(ll x){
int t = 0;
for(int i = ans-1; i >= 0; i--){
if(op[i] == 1)
x = fun1(x);
else if(op[i] == 2)
x = fun2(x);
else
t++;
}
for(int i = 1; i <= t; i++)
x = x*x%MOD; //题目第三个函数
return x;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
ans = 0;
cin>>n>>m;
for(int i = 0; i < m; i++){
char c;
int d;
cin>>c>>d;
if(c=='O')
op[ans++] = d;
else
cout<<solve((ll)d)<<endl;
}
}
return 0;
}