HDOJ 5475 An easy problem

 

题目传送门

题意:一个计算器,两种操作,乘上x,或者除掉之前的某个x,结果取模输出

分析:因为取模不支持除法,然后比赛时想到用逆元,结果发现MOD需要与b互质,结果一直苦苦寻找求逆元的其它方法。后来队友用暴力方法竟然水过,具体操作是记录每次乘的x,如果除的话,将对应的x 改为1,然后一个一个乘。当然正解应该用线段树,树的底部每个点表示每一次操作的x,pushup的是区间的乘积,如果是除把对应的x变为1,发现其实就是暴力的优化。。。。

 

/************************************************
* Author        :Running_Time
* Created Time  :2015/9/30 星期三 13:33:35
* File Name     :H_ST.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
int M;
int pos[N];
struct ST   {
    int v[N<<2];
    void push_up(int rt)    {
        v[rt] = (v[rt<<1] * 1ll * v[rt<<1|1]) % M;
    }
    void build(int l, int r, int rt)    {
        if (l == r) {
            v[rt] = 1;  return ;
        }
        int mid = (l + r) >> 1;
        build (lson);   build (rson);
        push_up (rt);
    }
    void updata(int p, int c, int l, int r, int rt) {
        if (l == r) {
            v[rt] = c;   return ;
        }
        int mid = (l + r) >> 1, ret = 1;
        if (p <= mid)   updata (p, c, lson);
        else    updata (p, c, rson);
        push_up (rt);
    }
}st;

int main(void)	{
	int T, cas = 0;	scanf ("%d", &T);
	while (T--)	{
		int Q;
		scanf ("%d%d", &Q, &M);
		printf ("Case #%d:\n", ++cas);
	    
        st.build (1, Q, 1);
        int p = 1;
        for (int op, x, i=1; i<=Q; ++i)    {
            scanf ("%d%d", &op, &x);
            if (op == 1)    {
                st.updata (p, x, 1, Q, 1);
                printf ("%d\n", st.v[1]);
                pos[i] = p++;
            }
            else    {
                st.updata (pos[x], 1, 1, Q, 1);
                printf ("%d\n", st.v[1]);
            }
        }
    }

    return 0;
}

  

posted @ 2015-09-30 14:06  Running_Time  阅读(210)  评论(0编辑  收藏  举报