ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)

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.

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

题目主要是出现的除法,在模条件下是不能进行除法的,除非存在逆元可以实现除法,但是此处除数不一定与被除数互质。

但是如果过程中不模的话,就要使用大数,会T。

考虑到题目中提到了,除数不会出现相同的。

也就是如果乘了1,2,3,然后再除掉2的话,结果就是由1和3构成,这样就不用考虑每个数的情况了,此时的每个数就是一个整体,结果只和这个数有没有出现有关。

于是可以考虑用线段树来维护分段的积。当某一个数被除掉了,所有与这个数相关的区间都要重新计算,最多有log(q)个区间。

这样效率就是qlogq,是满足条件的。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long

using namespace std;

const int maxN = 100005;
int q, m;
int op[maxN], top;

//线段树
struct node
{
    int lt, rt;
    LL val;
}tree[4*maxN];

//向上更新
void pushUp(int id)
{
    tree[id].val = (tree[id<<1].val*tree[id<<1|1].val)%m;
}

//建立线段树
void build(int lt, int rt, int id)
{
    tree[id].lt = lt;
    tree[id].rt = rt;
    tree[id].val = 1;//每段的初值,根据题目要求
    if (lt == rt)
    {
        //tree[id].add = ??;
        return;
    }
    int mid = (lt+rt)>>1;
    build(lt, mid, id<<1);
    build(mid+1, rt, id<<1|1);
    pushUp(id);
}

void add(int lt, int rt, int id, int pls)
{
    if (lt <= tree[id].lt && rt >= tree[id].rt)
    {
        if (pls)
        {
            tree[id].val *= pls;
            tree[id].val %= m;
        }
        else
            tree[id].val = 1;
        return;
    }
    int mid = (tree[id].lt+tree[id].rt)>>1;
    if (lt <= mid)
        add(lt, rt, id<<1, pls);
    if (rt > mid)
        add(lt, rt, id<<1|1, pls);
    pushUp(id);
}

void work()
{
    build(1, q, 1);
    top = 1;
    int d, y;
    for (int i = 0; i < q; ++i)
    {
        scanf("%d%d", &d, &y);
        if (d == 1)
            add(top, top, 1, y);
        else
            add(y, y, 1, 0);
        op[top++] = y;
        printf("%I64d\n", tree[1].val);
    }
}

int main()
{
    //freopen("test.in", "r", stdin);
    int T;
    scanf("%d", &T);
    for (int times = 1; times <= T; ++times)
    {
        printf("Case #%d:\n", times);
        scanf("%d%d", &q, &m);
        work();
    }
    return 0;
}
时间: 2024-10-17 00:20:31

ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)的相关文章

ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)

Problem Description On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician entered the cave because it is there. Somewhere deep in the cave, she found a treasure chest with a com

ACM学习历程——HDU5017 Ellipsoid(模拟退火)(2014西安网赛K题)

---恢复内容开始--- Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distance between the original point (0,0,0) and points on the ellipsoid. The distance between two points (x 1,y 1,z 1) and (x 2,y 2,z 2) is defined as  Input

ACM学习历程——HDU3333 Turing Tree(线段树 &amp;&amp; 离线操作)

Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because Turing Tree could easily have the solution. As well, wily 3xian made lots of new problems about intervals. So, today, this sick t

ACM学习历程—POJ1151 Atlantis(扫描线 &amp;&amp; 线段树)

Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend

hdu 5475 模拟计算器乘除 (2015上海网赛F题 线段树)

给出有多少次操作 和MOD 初始值为1 操作1 y 表示乘上y操作2 y 表示除以第 y次操作乘的那个数 线段树的叶子结点i 表示 第i次操作乘的数 将1替换成y遇到操作2 就把第i个结点的值 替换成1利用线段树的性质,对整个1~n的区间进行维护,每次输出sum[1]的值即可 Sample Input110 10000000001 22 11 21 102 32 41 61 71 122 7 Sample OutputCase #1:2122010164250484 1 # include <i

ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)

Problem Description In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with a1,a2,a3,...,an representing the size of the water source. Given a set of queries eac

ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)

Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujin

ACM学习历程—HDU5476 Explore Track of Point(平面几何)(2015上海网赛09题)

Problem Description In Geometry, the problem of track is very interesting. Because in some cases, the track of point may be beautiful curve. For example, in polar Coordinate system,ρ=cos3θ is like rose, ρ=1−sinθ is a Cardioid, and so on. Today, there

ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)

Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. In