描述
小豆现在有一个数 x x x,初始值为 1 1 1 。 小豆有 Q Q Q次操作,操作有两种类型:
1 1 1 m m m: x = x × m x=x×m x=x×m ,输出 x m o d M x mod M xmodM;
2 2 2 p o s pos pos: x = x / x=x/ x=x/ 第 p o s pos pos次操作所乘的数(保证第 p o s pos pos 次操作一定为类型 1 1 1,对于每一个类型 1 1 1的操作至多会被除一次),输出 x m o d M x mod M xmodM 。
输入
一共有 t t t 组输入。
对于每一组输入,第一行是两个数字 Q , M Q,M Q,M。
接下来 Q Q Q 行,每一行为操作类型 o p op op ,操作编号或所乘的数字 m m m(保证所有的输入都是合法的)。
输出
对于每一个操作,输出一行,包含操作执行后的 x m o d M x mod M xmodM的值
样例输入
1
10 1000000000
1 2
2 1
1 2
1 10
2 3
2 4
1 6
1 7
1 12
2 7
样例输出
2
1
2
20
10
1
6
42
504
84
提示
对于20%的数据, 1 ≤ Q ≤ 500 1≤Q≤500 1≤Q≤500;
对于100%的数据, 1 ≤ Q ≤ 1 0 5 , t ≤ 5 , M ≤ 1 0 9 1≤Q≤10^5, t≤5,M≤10^9 1≤Q≤105,t≤5,M≤109;
第一眼还以为高精度乘除取膜
瞬间放弃
第二眼线段树水题
维护一下区间积,每次除把一个点改回来就是了
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read(){
char ch=getchar();
int res=0,f=1;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
return res*f;
}
const int N=100005;
ll tr[N<<2],a[N],mod;
int T,n;
#define lc (u<<1)
#define rc ((u<<1)|1)
#define mid ((l+r)>>1)
inline void pushup(int u){
tr[u]=tr[lc]*tr[rc]%mod;
}
inline void buildtree(int u,int l,int r){
tr[u]=1;
if(l==r)return;
buildtree(lc,l,mid);
buildtree(rc,mid+1,r);
pushup(u);
}
inline void update(int u,int l,int r,int pos,ll k){
if(l==r){
tr[u]=k;return;
}
if(pos<=mid)update(lc,l,mid,pos,k);
else update(rc,mid+1,r,pos,k);
pushup(u);
}
inline void change(int u,int l,int r,int pos){
if(l==r){
tr[u]=1;return;
}
if(pos<=mid)change(lc,l,mid,pos);
else change(rc,mid+1,r,pos);
pushup(u);
}
int main(){
int T=read();
while(T--){
n=read(),mod=read();
buildtree(1,1,n);
for(int i=1;i<=n;i++){
int op=read(),k=read();
if(op==1){
update(1,1,n,i,k);
cout<<tr[1]<<'\n';
}
else {
change(1,1,n,k);
cout<<tr[1]<<'\n';
}
}
}
}