An easy problem
Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1137 Accepted Submission(s): 557
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≤10
5
,1≤M≤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≤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.
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≤10
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≤10
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
这是一道怎么说的题目呢。刚开始看到5000MS,直接暴力去做。
然后我得到了这样的一个思路:(暴力思路)
一步一步乘啊乘,乘啊乘,除啊除,除啊除。
然后感觉好二,因为我是和我队友一起做,他跟我提出个道理,除掉哪组数据,就相当于不乘这组数据。因为毕竟除数据是会有纰漏的,会有精度消耗之类的不可避免的问题。然后还是暴力解:乘啊乘乘啊乘,标记啊标记,标记啊标记。然后就TLE了~
后来发现,如果是求数据乘积,我可以利用线段树去做啊~然后就开始构思:因为是n组数据,一共所有数也就n个,因为是乘积,所以我初始化为1就行:
void pushup(int rt)
{
tree[rt]=(tree[rt<<1]%m*tree[rt<<1|1]%m)%m;//别忘记求余
}
void build( int l ,int r , int rt )
{
if( l == r )
{
tree[rt]=1;
return ;
}
else
{
int m = (l+r)>>1 ;
build(lson) ;
build(rson) ;
pushup(rt) ;
}
}
然后是数据修改:如果是操作1,表示要乘上这个数,对应树上的位子改变数据为要乘的数就行,相反的如果是操作2,表示有某一组数据相当于不乘,那么相对应的位子改为乘1就行了:
void update(int p,int c,int l,int r,int rt)//p表示位子,c表示更改数据
{
if(l==r)
{
tree[rt]=c;
}
else
{
int m=(l+r)>>1;
if(p<=m) update(p,c,lson);
else update(p,c,rson);
pushup(rt);
}
}
因为是求树中所有数的乘积,那么我们输出tree【1】就行了,这里是完整AC代码:
#include<stdio.h>
#include<string.h>
using namespace std;
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
__int64 tree[1212121];
int n,m;
void pushup(int rt)
{
tree[rt]=(tree[rt<<1]%m*tree[rt<<1|1]%m)%m;
}
void build( int l ,int r , int rt )
{
if( l == r )
{
tree[rt]=1;
return ;
}
else
{
int m = (l+r)>>1 ;
build(lson) ;
build(rson) ;
pushup(rt) ;
}
}
void update(int p,int c,int l,int r,int rt)
{
if(l==r)
{
tree[rt]=c;
}
else
{
int m=(l+r)>>1;
if(p<=m) update(p,c,lson);
else update(p,c,rson);
pushup(rt);
//printf("sum[%d]:%d\n",rt,tree[rt]);
}
}
int main()
{
int t;
int kase=0;
scanf("%d",&t);
while(t--)
{
memset(tree,0,sizeof(tree));
scanf("%d%d",&n,&m);
build(1,n,1);
printf("Case #%d:\n",++kase);
for(int i=0;i<n;i++)
{
int q,c;
scanf("%d%d",&q,&c);
if(q==1)
{
update(i+1,c,1,n,1);
printf("%I64d\n",tree[1]%m);
}
if(q==2)
{
update(c,1,1,n,1);
printf("%I64d\n",tree[1]%m);
}
}
}
}