思路:正着暴力会tle,所以我们可以逆着来。
代码:
#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
#define endl '\n'
#define pq priority_queue
using namespace std;
typedef pair<int,int> pii;
void solve(){
int n, m;
cin >> n >> m;
deque<int>a;
string s;
int sum = 1;
for(int i = 0;i < n;i ++){
int x;
cin >> x;
a.push_back(x);
}
cin >> s;
vector<int>ans;
for(int i = 0;i < n;i ++){
if(s[i] == 'L'){
int x = a.front();
a.pop_front();
ans.push_back(x);
}else{
int x = a.back();
a.pop_back();
ans.push_back(x);
}
}
vector<int>res;
for(int i = n - 1;i >= 0;i --){
sum = sum * ans[i] % m;
res.push_back(sum);
}
for(int i = n - 1;i >= 0;i --){
cout << res[i] << ' ';
}
cout << endl;
}
signed main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t;cin>>t;
while(t--)
{
solve();
}
return 0;
}