One day, a useless calculator was being built by Kuros. Let's assume that number X is showed on the screen of calculator. At first, X = 1. This calculator only supports two types of operation.
1. multiply X with a number.
2. divide X with a number which was multiplied before.
After each operation, please output the number X modulo M.
Input
The first line is an integer T(1≤T≤10
), indicating the number of test cases.
For each test case, the first line are two integers Q and M. Q is the number of operations and M is described above. (1≤Q≤105,1≤M≤109)
The next Q lines, each line starts with an integer x indicating the type of operation.
if x is 1, an integer y is given, indicating the number to multiply. (0<y≤109
)
if x is 2, an integer n is given. The calculator will divide the number which is multiplied in the nth operation. (the nth operation must be a type 1 operation.)
It's guaranteed that in type 2 operation, there won't be two same n.
Output
For each test case, the first line, please output "Case #x:" and x is the id of the test cases starting from 1.
Then Q lines follow, each line please output an answer showed by the calculator.
Sample Input
1 10 1000000000 1 2 2 1 1 2 1 10 2 3 2 4 1 6 1 7 1 12 2 7
Sample Output
Case #1: 2 1 2 20 10 1 6 42 504 84
首先,这题数据水了,暴力竟然可以。
其次,这个题就是一个基础的线段树,区间记(l,r)操作的结果。
对于操作1,先单点更新为y,再区间查询。
对于操作2,先单点更新为1,再区间查询即可。
(PS:这题也是不知道为啥,比赛的时候没有想起来,明明就是一个很明显的线段树。以前也算做过线段树的基础题,虽然好久没写了,但就没认真往线段树想过,确实菜。。。。。。。。。。。。。。。。。。。)
1.暴力
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N =1e5+10;
ll y[N];
bool vis[N];
int main(void)
{
int t,tt=0,q,m,type;
ll ans;
scanf("%d",&t);
while(t--)
{
ans=1;
scanf("%d%d",&q,&m);
for(int i=1;i<=q;i++)
vis[i]=0;
printf("Case #%d:\n",++tt);
for(int i=1;i<=q;i++)
{
scanf("%d%lld",&type,&y[i]);
if(type==1)
{
vis[i]=1;
ans=ans*y[i]%m;
printf("%lld\n",ans);
}
else
{
ans=1;
vis[y[i]]=0;
for(int j=1;j<i;j++)
{
if(vis[j]) ans=ans*y[j]%m;
}
printf("%lld\n",ans);
}
}
}
return 0;
}
2.线段树
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+10;
struct node
{
int l,r;
ll val;
}tree[N<<2];
ll mod;
void build(int l,int r,int cur)
{
tree[cur].l=l;
tree[cur].r=r;
tree[cur].val=1;
if(l==r)
{
return ;
}
int m=(l+r)/2;
build(l,m,2*cur);
build(m+1,r,2*cur+1);
return ;
}
void pushup(int cur)
{
tree[cur].val=(tree[2*cur].val*tree[2*cur+1].val)%mod;
return ;
}
ll query(int l,int r,int pl,int pr,int cur)
{
ll res=1;
if(pl>=l&&pr<=r)
{
return tree[cur].val%mod;
}
int m=(pl+pr)/2;
if(m>=l) res=(res*query(l,r,pl,m,2*cur))%mod;
if(m<r) res=(res*query(l,r,m+1,pr,2*cur+1))%mod;
return res;
}
void update(ll tar,ll va,int cur)
{
if(tree[cur].l==tree[cur].r)
{
tree[cur].val=va;
return ;
}
if(tar<=tree[2*cur].r) update(tar,va,2*cur);
else update(tar,va,2*cur+1);
pushup(cur);
return ;
}
int main(void)
{
int t,tt=0,op;
ll y;
scanf("%d",&t);
while(t--)
{
int q,ans;
scanf("%d%lld",&q,&mod);
build(1,q,1);
printf("Case #%d:\n",++tt);
for(int i=1;i<=q;i++)
{
scanf("%d%lld",&op,&y);
if(op==1)
{
update(i,y,1);
printf("%lld\n",query(1,i,1,q,1));
}
else
{
update(y,1,1);
printf("%lld\n",query(1,i,1,q,1)%mod);
}
}
}
return 0;
}