HDU 5475 An easy problem 线段树

An easy problem

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5475

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.

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

HINT

题意

一开始ans = 1

有两个操作

1.乘以x

2.除以第y个加入的数

然后需要mod

题解:

显然,不用mod的话,就是傻逼题了

但是有一个mod,那么我求逆元就好了

但是很蛋疼的是,有些数并没有逆元怎么办?

那就线段树咯……

HDU 5475 An easy problem 线段树

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1) using namespace std;
const int maxn = 1e5 + ;
int Q;
long long MOD,X,C[maxn]; typedef long long SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum ;
void updata(SgTreeDataType v)
{
sum = v;
}
}; treenode tree[maxn*];
inline void push_up(int o)
{
tree[o].sum = (tree[*o].sum * tree[*o+].sum)%MOD;
} inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum = 1LL;
if (R > L)
{
int mid = (L+R) >> ;
build_tree(L,mid,o*);
build_tree(mid+,R,o*+);
}
} inline void updata(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].updata(v);
else
{
int mid = (L+R)>>;
if (QL <= mid) updata(QL,QR,v,o*);
if (QR > mid) updata(QL,QR,v,o*+);
push_up(o);
}
} inline SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
int mid = (L+R)>>;
SgTreeDataType res = 1LL;
if (QL <= mid) res *= query(QL,QR,*o);
if(res >= MOD) res %= MOD;
if (QR > mid) res *= query(QL,QR,*o+);
if(res >= MOD) res %= MOD;
return res;
}
} void initiaiton()
{
X=;
scanf("%d%I64d",&Q,&MOD);
build_tree(,Q,);
} void solve()
{
for(int i = ; i <= Q ; ++ i)
{
int type ;
long long y;
scanf("%d%I64d",&type,&y);
if(type == )
{
updata(i , i , y , );
printf("%I64d\n",query(,Q,));
}
else
{
updata(y , y , , );
long long cx = query(,Q,);
printf("%I64d\n",cx);
X = cx;
}
}
} int main(int argc,char *argv[])
{
int Case;
scanf("%d",&Case);
int cas=;
while(Case--)
{
initiaiton();
printf("Case #%d:\n",cas++);
solve();
}
return ;
}
上一篇:UVA 11991 Easy Problem from Rujia Liu?(vector map)


下一篇:JS函数与BOM