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.
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.
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 \leq Q \leq 10^5, 1 \leq M \leq 10^9$)
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 \leq 10^9$)
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.
Then Q lines follow, each line please output an answer showed by the calculator.
1 10 1000000000 1 2 2 1 1 2 1 10 2 3 2 4 1 6 1 7 1 12 2 7
Case #1: 2 1 2 20 10 1 6 42 504 84
题意是每组测试给出Q,M,Q是查询数,每组查询给出op,n,有x初始值为1,若op=1则x乘上n,若op=2则x除以第n个查询中乘的数(这里对应的一定是op=1的n),每组查询输出X%M,
可用线段树,单点更新,区间查询,区间存放的是区间内元素的积
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 100005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
ll q,mod;
ll tree[M<<2];
void pushup(ll rt)
{
tree[rt]=(tree[rt<<1]*tree[rt<<1|1])%mod;
}
void build(ll l,ll r,ll rt)
{
if(l==r)
{
tree[rt]=1;
return;
}
ll m=(l+r)>>1;
build(lson);
build(rson);
tree[rt]=1;
}
void update(ll p,ll v,ll l,ll r,ll rt)
{
if(l==r)
{
tree[rt]=v;
return;
}
ll m=(l+r)>>1;
if(p<=m)
update(p,v,lson);
else
update(p,v,rson);
pushup(rt);
}
int main()
{
int T,cas=0;
ll op,n,i,v,p;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&q,&mod);
build(1,q,1);
//memset(tree,1,sizeof(tree)); //longlong不能用memset?
printf("Case #%d:\n",++cas);
for(i=1;i<=q;i++)
{
scanf("%lld%lld",&op,&n);
if(op==1)
{
p=i;
v=n;
}else
{
p=n;
v=1;
}
update(p,v,1,q,1);
printf("%lld\n",tree[1]);
}
}
return 0;
}
还可以直接模拟,注意因为题目中不断对x取余所以进行除法时已经我发得到正确的值,所以遇到除法从头开始乘就行。
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 100005
ll q,m;
ll number[M];
bool vis[M];
int main()
{
int T,cas=0;
ll op,n,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&q,&m);
printf("Case #%d:\n",++cas);
ll x=1;
memset(vis,0,sizeof(vis));
for(i=1;i<=q;i++)
{
scanf("%lld%lld",&op,&n);
number[i]=n;
if(op==1)
{
vis[i]=true;
x=(x*n)%m; //计算%是很费时的,即如果这里换为x=(x%m*n%m)%m,会处在超时边缘
}else
{
x=1;
for(j=1;j<i;j++)
{
if(j==n)
vis[j]=false;
else
{
if(vis[j]==true)
x=(x*number[j])%m;
}
}
}
printf("%lld\n",x);
}
}
return 0;
}