LR-remainders
Problem
You are given an array a of length n, a positive integer m, and a string of commands of length n. Each command is either the character ‘L’ or the character ‘R’.
Process all n commands in the order they are written in the string s. Processing a command is done as follows:
First, output the remainder of the product of all elements of the array a when divided by m.
Then, if the command is ‘L’, remove the leftmost element from the array a, if the command is ‘R’, remove the rightmost element from the array a.
Note that after each move, the length of the array a decreases by 1, and after processing all commands, it will be empty.
Write a program that will process all commands in the order they are written in the string s (from left to right).
Input
The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 ) (1≤t≤10^4) (1≤t≤104) — the number of test cases in the input. Then descriptions of t test cases follow.
Each test case of the input is given by three lines.
The first line contains two integers n n n and m m m ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ m ≤ 1 0 4 ) (1≤n≤2⋅10^5,1≤m≤10^4) (1≤n≤2⋅105,1≤m≤104) — the initial length of the array a and the value to take the remainder by.
The second line contains n integers a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,…,an ( 1 ≤ a i ≤ 1 0 4 ) (1≤a_i≤10^4) (1≤ai≤104) — the elements of the array a.
The third line contains a string s consisting of n characters ‘L’ and ‘R’.
It is guaranteed that the sum of the values of n for all test cases in a test does not exceed 2 ⋅ 1 0 5 2⋅10^5 2⋅105.
Output
For each test case, output n integers b 1 , b 2 , … , b n b_1,b_2,…,b_n b1,b2,…,bn, where b i b_i bi is the remainder when dividing the product of all elements of the current state of the array a by m at the beginning of the execution of the i-th command.
Example
Input
4
4 6
3 1 4 2
LRRL
5 1
1 1 1 1 1
LLLLL
6 8
1 2 3 4 5 6
RLLLRR
1 10000
10000
R
Output
0 2 4 1
0 0 0 0 0
0 0 0 4 4 4
0
Note
In the first test case of the example:
- 3 ⋅ 1 ⋅ 4 ⋅ 2 3⋅1⋅4⋅2 3⋅1⋅4⋅2 m o d mod mod 6 = 24 6=24 6=24 m o d mod mod 6 = 0 6=0 6=0;
- s 1 = L s_1=L s1=L, so we remove the first element and get the array [ 1 , 4 , 2 ] [1,4,2] [1,4,2];
- 1 ⋅ 4 ⋅ 2 1⋅4⋅2 1⋅4⋅2 m o d mod mod 6 = 8 6=8 6=8 m o d mod mod 6 = 2 6=2 6=2;
- s 2 = R s_2=R s2=R, so we remove the last element and get the array [ 1 , 4 ] [1,4] [1,4];
- 1 ⋅ 4 1⋅4 1⋅4 m o d mod mod 6 = 4 6=4 6=4 m o d mod mod 6 = 4 6=4 6=4;
- s 3 = R s_3=R s3=R, so we remove the last element and get the array [ 1 ] [1] [1];
- 1 1 1 m o d mod mod 6 = 1 6=1 6=1;
- s 4 = L s_4=L s4=L, so we remove the first element and get an empty array.
Code
// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <sstream>//整型转字符串
// #include <stack>//栈
// #include <deque>//堆/优先队列
// #include <queue>//队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=2e5+10;
ll a[N],b[N],ans[N];
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n,m;
cin>>n>>m;
for(ll i=1;i<=n;i++)
cin>>a[i];
string s;
cin>>s;
ll l=1,r=n,j=0;
for(auto &t:s)
{
if(t=='L') b[++j]=a[l++];
else b[++j]=a[r--];
}
ll sum=1;
for(ll i=n;i>=1;i--)
{
sum=sum*b[i]%m;
ans[i]=sum;
}
for(ll i=1;i<=n;i++)
cout<<ans[i]<<" ";
cout<<endl;
}
return 0;
}