Given a integers x = 1, you have to apply Q (Q ≤ 100000) operations: Multiply, Divide.
Input
First line of the input file contains an integer T(0 < T ≤ 10) that indicates how many cases of inputs are there.
The description of each case is given below:
The first line contains two integers Q and M. The next Q lines contains the operations in ith line following form:
M yi: x = x * yi.
N di: x = x / ydi.
It’s ensure that di is different. That means you can divide yi only once after yi came up.
0 < yi ≤ 10^9, M ≤ 10^9
Output
For each operation, print an integer (one per line) x % M.
Sample Input
1 10 1000000000 M 2 D 1 M 2 M 10 D 3 D 4 M 6 M 7 M 12 D 7
Sample Output
2 1 2 20 10 1 6 42 504 84
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 1e5+1;
ll tree1[maxn*4];
ll value;
ll mod;
void update(int l,int r,int root,int k)
{
if(l==r)
{
tree1[root]=value;
return;
}
ll mid=(l+r)/2;
if(k<=mid)
update(l,mid,root*2,k);
else
update(mid+1,r,root*2+1,k);
tree1[root]=tree1[root*2]*tree1[root*2+1]%mod;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
int k;
scanf("%d %lld",&n,&mod);
char s[100];
for(ll i=1; i<=maxn*4; i++)
tree1[i]=1;
for(int i=1; i<=n; i++)
{
scanf("%s",s);
scanf("%d",&k);
if(s[0]=='M')
{
value=k;
update(1,maxn,1,i);
}
else
{
value=1;
update(1,maxn,1,k);
}
printf("%lld\n",tree1[1]);
}
}
return 0;
}