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 = ;
int q, m;
int op[maxN], top; //线段树
struct node
{
int lt, rt;
LL val;
}tree[*maxN]; //向上更新
void pushUp(int id)
{
tree[id].val = (tree[id<<].val*tree[id<<|].val)%m;
} //建立线段树
void build(int lt, int rt, int id)
{
tree[id].lt = lt;
tree[id].rt = rt;
tree[id].val = ;//每段的初值,根据题目要求
if (lt == rt)
{
//tree[id].add = ??;
return;
}
int mid = (lt+rt)>>;
build(lt, mid, id<<);
build(mid+, rt, id<<|);
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 = ;
return;
}
int mid = (tree[id].lt+tree[id].rt)>>;
if (lt <= mid)
add(lt, rt, id<<, pls);
if (rt > mid)
add(lt, rt, id<<|, pls);
pushUp(id);
} void work()
{
build(, q, );
top = ;
int d, y;
for (int i = ; i < q; ++i)
{
scanf("%d%d", &d, &y);
if (d == )
add(top, top, , y);
else
add(y, y, , );
op[top++] = y;
printf("%I64d\n", tree[].val);
}
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
printf("Case #%d:\n", times);
scanf("%d%d", &q, &m);
work();
}
return ;
}
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 discovere ...
- ACM学习历程——HDU5017 Ellipsoid(模拟退火)(2014西安网赛K题)
---恢复内容开始--- Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distanc ...
- ACM学习历程——HDU3333 Turing Tree(线段树 && 离线操作)
Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...
- ACM学习历程—POJ1151 Atlantis(扫描线 && 线段树)
Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...
- hdu 5475 模拟计算器乘除 (2015上海网赛H题 线段树)
给出有多少次操作 和MOD 初始值为1 操作1 y 表示乘上y操作2 y 表示除以第 y次操作乘的那个数 线段树的叶子结点i 表示 第i次操作乘的数 将1替换成y遇到操作2 就把第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 ...
- ACM学习历程—HDU5476 Explore Track of Point(平面几何)(2015上海网赛09题)
Problem Description In Geometry, the problem of track is very interesting. Because in some cases, th ...
- ACM学习历程—HDU5478 Can you find it(数论)(2015上海网赛11题)
Problem Description Given a prime number C(1≤C≤2×105), and three integers k1, b1, k2 (1≤k1,k2,b1≤109 ...
- ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)
Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...
随机推荐
- Leetcode - CopyWithRandomList
Algorithm: Iterate and copy the original list first. For the random pointer, just copy the value fro ...
- lua面向对象铺垫
Account = { balance = , withdraw = function(self, v) self.balance = self.balance - v end } --:操作符隐藏了 ...
- Office 365系列(-)
昨天参加上海微软TechED技术大会,看见很多传说中的大牛,听了涂曙光老师等人的讲座,激情澎湃啊,看见他们对技术以及程序员社区的投入及激情,十分敬佩.自己搞IT行业也已经10多年了,平常都很少写博客和 ...
- Just a Hook(线段树)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- ACM暑假集训第三周小结
这一周学的图论,学了这么些 两种存图的方法:邻接矩阵( map[n][n] ) , 邻接表( headlis[n] , vector<int> G[n] )存图的方法,各有各的好,我的理解 ...
- java操作文件流对象
所有流对象 InputStream 字节流 FileInputStream 字节流 专门读写非文本文件的 BufferedInputStream 高效流 OutPutS ...
- This instability is a fundamental problem for gradient-based learning in deep neural networks. vanishing exploding gradient problem
The unstable gradient problem: The fundamental problem here isn't so much the vanishing gradient pro ...
- vue表单输入的绑定
vue的核心:声明式的指令和数据的双向绑定. 那么声明式的指令,已经给大家介绍完了.接下来我们来研究一下什么是数据的双向绑定? 另外,大家一定要知道vue的设计模式:MVVM M是Model的简写,V ...
- 7.Django模型类的定义和管理
Django的模型类是给ORM层服务的 1.每个数据模型都是django.db.models.Model的子类. 2.它的父类Model包含了所有必要的和数据库交互的方法,并提供了定义数据库字段的语法 ...
- Js中的Object.defineProperty
通过Object.defineProperty为对象设置属性,并同时规定属性的属性(可见性,可配置性,可枚举性等) 备注:如果通过var obj = {} obj.age = 18这种方式设置的属性, ...