Problem Description
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.
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.
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.
Then Q lines follow, each line please output an answer showed by the calculator.
题目大意:给X赋初值1,然后给Q个操作,每个操作对应一个整数M;如果操作是1则将X乘以对应的M,如果是2则除以第M次操作对应的M',求最后X的值对给定值取摸的结果。
第一眼感觉暴力可过,WA
重写后因对同余定理理解不深WA
再重写因为优化不够,一直TLE
比完后才想到可以用线段树优化
线段树,记录对应的乘和除,每次操作更新单点,然后求区间乘积 。
代码:
#include<stdio.h>
#include<iostream>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1 | 1
using namespace std;
const int maxn=100005;
const int inf=1<<29;
int n,m,t;
long long sum[500000],M;
void pushup(int rt)
{
sum[rt]=sum[rt<<1]*sum[rt<<1|1]%M;
}
void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=1;
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int i,int add,int l,int r,int rt)
{
if(l==r)
{
sum[rt]=add;
return ;
}
int m=(l+r)>>1;
if(i<=m)
update(i,add,lson);
else
update(i,add,rson);
pushup(rt);
}
long long query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return sum[rt];
}
int m=(l+r)>>1;
long long res=1;
if(L<=m)
res=(res*query(L,R,lson))%M;
if(R>m)
res=(res*query(L,R,rson))%M;
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin>>t;
for(int p=1;p<=t;p++)
{
cout<<"Case #"<<p<<":"<<endl;
cin>>n>>M;
build(1,n,1);
for(int i=1;i<=n;i++)
{
int op,x;
cin>>op>>x;
if(op==1)
{
update(i,x,1,n,1);
cout<<query(1,i,1,n,1)<<endl;
}
else
{
update(x,1,1,n,1);
cout<<query(1,i,1,n,1)<<endl;
}
}
}
return 0;
}