LR-remainders

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) (1t104) — 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) (1n2105,1m104) — 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) (1ai104) — 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 2105.

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 3142 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 142 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 14 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;
}
  • 36
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pretty Boy Fox

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值