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;
}

  

时间: 2024-09-29 15:38:32

HDOJ 5475 An easy problem的相关文章

HDU 5475 An easy problem(线段树)

题目链接: 戳我 题目大意: 一个计算器只有两种运算,初始化 X = 1 第一种操作: X 乘以 一个数,获得新的 X 第二种操作: 当前的 X 除以 一个数 输出  X % M 其中 1 y 表示第一种操作,即 X  = X * y 2 n 表示 X 除以 前面的 第 n 个操作的那个 y,保证 第 n 个表示的是 1,即数据合法 注意:X 在进行操作的时候不要除模, 而是输出的时候除模,即要保证 X 进行操作的时候除模是和不除模情况下是相等的. 比如: 3 10 1 5 1 3 2 1,如果

2015上海网络赛 HDU 5475 An easy problem 线段树

题意就不说了 思路:线段树,维护区间乘积.2操作就将要除的点更新为1. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #include<vector> 8 #include<set> 9 #include<stri

an easy problem(贪心)

An Easy Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8333   Accepted: 4986 Description As we known, data stored in the computers is in binary form. The problem we discuss now is about the positive integers and its binary form.

HDU2123 An easy problem【水题】

An easy problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5922    Accepted Submission(s): 4122 Problem Description In this problem you need to make a multiply table of N * N ,just like th

HDU2132 An easy problem【水题】

An easy problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10149    Accepted Submission(s): 2689 Problem Description We once did a lot of recursional problem . I think some of them is easy

poj2826An Easy Problem?!

链接 繁琐细节题. 1.线段无交点时,ans=0; 2.如图 假设过p3.y的水平线与p1p2相交 因为雨是垂直下落的,左图的情况是无法收集到雨水的 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath&g

Poj 2826 An Easy Problem?!

地址:http://poj.org/problem?id=2826 题目: An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 2003 Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Ben nails tw

FZU 1753-Another Easy Problem(求多个组合数的最大公约数)

Another Easy Problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice FZU 1753 Appoint description:  xietutu  (2013-03-13)System Crawler  (2015-04-27) Description 小TT最近学习了高斯消元法解方程组,现在他的问题来了,如果是以下的方程,

(DS 《算法入门经典》)UVA 11991 Easy Problem from Rujia Liu?(求第k个v出现的索引)

题目大意: 求第k个v出现的索引 解题思路: 如果能构造出一个数据结构,使得data[v][k]就是第k个v出现的索引值即可求解.data[v]表示数v出现的索引数组, data[v][k]表示第k个v出现的索引. Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 200