An easy problem
Time Limit:1 Sec
Memory limit:256 MB
Topic Connection http://acm.hdu.edu.cn/showproblem.php?pid=5475
Descriptionone Day, a useless calculator is being built by Kuros. Let's assume that number X was showed on the screen of calculator. At first, X = 1. This calculator only supports and types of operation.
1. Multiply X with a number.
2. Divide X with a number which is multiplied before.
After all operation, please output the number X modulo m.input
The first line was an integer T (1≤t≤10), indicating the number of test cases.
For each test case, the first line is both 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, a integer y is given, indicating the number to multiply. (0<y≤109)
If X is 2, an integer n is given. The calculator would divide the number which is multiplied in the nth operation. (The nth operation must be a type 1 operation.)
It's guaranteed that's in type 2 operation, there won ' t is the same n.
Output
For each test case, the first line, click Output "Case #x:" and X are the ID of the the test cases starting from 1.
Then Q lines follow, each of the line "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
Test instructions
First ans = 1
There are two operations
1. Multiply by X
2. Divide by the number of y added
Then you need mod.
Exercises
Obviously, without mods, it's a stupid question.
But there is a mod, then I beg the inverse of the yuan.
But the very egg hurts is, some number does not have the inverse yuan to do?
That's the line tree.
Code:
#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 unsignedChar byte;#definePB Push_back#defineInput_fast Std::ios::sync_with_stdio (false); Std::cin.tie (0)#defineLocal Freopen ("In.txt", "R", stdin)#definePi ACOs (-1)using namespacestd;Const intMAXN = 1e5 + -;intQ;Long LongMod,x,c[maxn];typedefLong LongSgtreedatatype;structtreenode{intL, R; Sgtreedatatype sum; voidUpdata (Sgtreedatatype v) {sum=v; }};treenode TREE[MAXN*4];inlinevoidPUSH_UP (into) {Tree[o].sum= (tree[2*o].sum * tree[2*o+1].sum)%MOD;} InlinevoidBuild_tree (intJ JintRinto) {Tree[o]. L= L, Tree[o]. R = R,tree[o].sum =1LL; if(R >L) {intMid = (l+r) >>1; Build_tree (L,mid,o*2); Build_tree (Mid+1, r,o*2+1); }}inlinevoidUpdata (intQlintQr,sgtreedatatype V,into) { intL = Tree[o]. L, R =Tree[o]. R if(QL <= L && R <=QR) Tree[o].updata (v); Else { intMid = (l+r) >>1; if(QL <= mid) Updata (ql,qr,v,o*2); if(QR > Mid) updata (ql,qr,v,o*2+1); PUSH_UP (o); }}inline sgtreedatatype Query (intQlintQrinto) { intL = Tree[o]. L, R =Tree[o]. R if(QL <= L && R <= QR)returntree[o].sum; Else { intMid = (l+r) >>1; Sgtreedatatype Res=1LL; if(QL <= mid) Res *= query (QL,QR,2*o); if(res >= MOD) Res%=MOD; if(QR > Mid) Res *= query (QL,QR,2*o+1); if(res >= MOD) Res%=MOD; returnRes; }}voidInitiaiton () {X=1; scanf ("%d%i64d",&q,&MOD); Build_tree (1Q1);}voidsolve () { for(inti =1; I <= Q; ++i) {inttype; Long Longy; scanf ("%d%i64d",&type,&X); if(Type = =1) {Updata (I, I, Y,1); printf ("%i64d\n", Query (1Q1)); } Else{updata (y, y,1,1); Long LongCX = Query (1Q1); printf ("%i64d\n", CX); X=CX; } }}intMainintargcChar*argv[]) { intCase ; scanf ("%d",&Case ); intcas=1; while(case--) {Initiaiton (); printf ("Case #%d:\n", cas++); Solve (); } return 0;}
HDU 5475 An easy problem segment tree