An easy problem
Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1324 Accepted Submission(s): 622
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.
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
每次询问有两个数x,y。当x=1时,求当前的X乘以y的结果
当x==2,求当前的X除以第y次询问的y值(数据保证当x=2时,第y次询问对应的是一个1询问)
每次询问输出操作完后的X
相信很多人一开始都跟我一样想到暴力- - 因为直观看上去暴力确实不会超时,可是问题在于每次都需要取模,且x=2的时候是一个除法取模,那么我们就会想到逆元,可是因为m是给的1~10^9,并不能保证一定互素,所以求不了逆元。 那么这种解法自然也就不了了之了。
然后对于多个数相乘求总的乘积,你想到什么? 没错,线段树。可是会有一些数需要除掉啊,那怎么办? 你想啊,我们把需要除掉的数的权值赋为1,再更新一遍树。
这样不就相当于从来没乘过这个数了吗,那也就是除法了。而且这样维护,每次所做的操作都是乘法操作,取模自然也是没问题的。
有人可能还会问,我现在把一个数的权值赋为1,会不会影响后面的操作? 答案是不会,我们的X是根据一步步询问求过去的,如果前面删除了一个数,在后面的结果中这个数依然会被删除。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100005
#define LL long long
LL tree[N<<2];
LL ll,rr,m;
LL anss;
void build(LL root,LL l,LL r)
{
tree[root]=1;
if(l==r)
return;
LL mid=(l+r)>>1;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
}
void pushup(LL root)
{
tree[root]=tree[root<<1]*tree[root<<1|1]%m;
}
void update(LL root,LL l,LL r,LL id,LL v)
{
if(l==r)
{
if(l==id) tree[root]=v;
return;
}
LL mid=(l+r)>>1;
if(mid>=id) update(root<<1,l,mid,id,v);
else update(root<<1|1,mid+1,r,id,v);
pushup(root);
}
int main()
{
LL T,q;
LL n,y;
scanf("%lld",&T);
for(int t=1;t<=T;t++)
{
scanf("%lld %lld",&n,&m);
printf("Case #%d:\n",t);
build(1,1,n);
for(int i=1;i<=n;i++)
{
scanf("%lld %lld",&q,&y);
if(q==1) update(1,1,n,i,y);
else update(1,1,n,y,1);
printf("%lld\n",tree[1]);
}
}
return 0;
}