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 ...
随机推荐
- OpenCV玩耍(一)批量resize一个文件夹里的所有图像
鉴于用caffe做实验的时候,里面牵扯到一个问题是必须将训练集和测试集都转成256*256的图像,而官网给出的代码又不会用,所以我用opencv转了.其实opencv只转一幅图会很简单,关键在于“批量 ...
- Robbery(记忆化搜索)
Robbery Inspector Robstop is very angry. Last night, a bank has been robbed and the robber has not b ...
- csv文件的格式
csv, comma separated values csv是一种纯文本文件. csv文件由任意数目的记录构成,记录间以换行符分割,每条记录由字段构成,字段间以逗号作为分隔符. 如果字段中有逗号,那 ...
- 我的Android进阶之旅------>四种呼叫转移场景
运行商为我们提供了如下4中呼叫转移场景: 1.始终进行呼叫转移:不管当前手机处于何种状态,来电都会被转移到指定的电话号码上.在使用这种呼叫转移时应当非常小心,如果启用了这种呼叫转移,你可就永远也接不着 ...
- 几款Java常用基础工具库
通用工具类(字符串.时间格式化.BeanUtils.IO) 1. commons-lang3库 1.1. org.apache.commons.lang3.StringUtils类 日常代码中,我们经 ...
- 变量动态选取资源ID
1.使用Resources 类的 getIdentifier方法 Resources res=getResources(); return res.getIdentifier(type ...
- 用Visual Studio编辑Linux代码
估计很多人都是用惯了Visual Studio的主,怎么也不适应Linux的一套编辑器,比如vim.source insight这些东西,可视化的eclipse效果还好点,但一般以远程共享一台Linu ...
- python基础10 ---匿名函数和递归
一.匿名函数 1.lambda表达式就相当于匿名函数,其格式为: lambda 参数列表:参数表达式 2.lambda自带return值,因为匿名函数有个限制,就是只能有一个表达式,不用写return ...
- Git——基本思想和工作原理(二)
核心知识点: 1.Git关注文件数据的整体是否发生变化,对更新的文件做一个快照,然后保存一个指向快照的索引,而不会关注文件数据的具体变化. 2.Git版本的更新几乎都发生在本地,不会因为没有网络而不能 ...
- leetcode 1049 Last Stone Weight II(最后一块石头的重量 II)
有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出任意两块石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == y,那么 ...