【HDU - 5475】An easy problem(线段树,思维)

题干:

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≤101≤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≤1091≤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≤1090<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

题目大意:

初始X=1;

给n,mod   ,表示n次操作

操作1格式 : 1  b 表示用X乘上b

操作2格式: 2   n 表示 当前X除掉第n次 "1操作” 的数

每次操作都要输出一个答案,输出的答案是要对mod取模

解题报告:

由于有除法所以我们不能每一步取模,因为对于每一个数对mod并不是都存在逆元。

考虑线段树维护乘积,下标代表第几次操作,所以直接做就可以了。对于第i次操作,如果是1 b操作就是在下标为i的地方更新为b,如果是2 n 操作就是在下标是n的地方更新值为1,线段树维护区间积就可以了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 4e5 + 5;
ll mod;
struct TREE {int l,r;ll val;
} tr[MAX];
void pushup(int cur) {tr[cur].val = tr[cur*2].val * tr[cur*2+1].val % mod;
}
void build(int l,int r,int cur) {tr[cur].l = l;tr[cur].r = r;tr[cur].val = 1;if(l == r) {return;}int m = (l+r)>>1;build(l,m,cur*2);build(m+1,r,cur*2+1);pushup(cur); 
}
void update(int tar,int cur,ll val) {if(tr[cur].l == tr[cur].r) {tr[cur].val = val;return;}if(tar <= tr[cur*2].r) update(tar,cur*2,val);else update(tar,cur*2+1,val);pushup(cur);
}
int main()
{int t,iCase=0,op,m;ll x;cin>>t;while(t--) {scanf("%d%lld",&m,&mod);build(1,m,1);printf("Case #%d:\n",++iCase);for(int i = 1; i<=m; i++) {scanf("%d%lld",&op,&x);if(op == 1) {update(i,1,x);printf("%lld\n",tr[1].val%mod);}else {update(x,1,1);printf("%lld\n",tr[1].val%mod);}}}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440167.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

图解算法学习笔记(六):广度优先搜索

目录 1&#xff09;图简介 2&#xff09;图是什么 3&#xff09;广度优先搜索 4&#xff09;实现图 5&#xff09;实现算法 6&#xff09;小结 本章内容; 学习使用新的数据结构图来建立网络模型&#xff1b; 学习广度优先搜索&#xff1b; 学习有向图和无向图…

图解算法学习笔记(七):狄克斯特拉算法

目录 1&#xff09;使用狄克斯特拉算法 2&#xff09;术语 3&#xff09;实现 4&#xff09;小结 本章内容; 介绍加权图&#xff0c;提高或降低某些边的权重&#xff1b; 介绍狄克斯特拉算法&#xff0c;找出加权图中前往X的最短路径&#xff1b; 介绍图中的环…

【HDU - 5477】A Sweet Journey(思维,水题)

题干&#xff1a; Master Di plans to take his girlfriend for a travel by bike. Their journey, which can be seen as a line segment of length L, is a road of swamps and flats. In the swamp, it takes A point strengths per meter for Master Di to ride; In the f…

[转载]Bluetooth协议栈学习之SDP

原文地址&#xff1a;Bluetooth协议栈学习之SDP作者&#xff1a;BigSam78作者&#xff1a; Sam &#xff08;甄峰&#xff09; sam_codehotmail.com SDP(service discovery protocol&#xff1a;服务发现协议)提供了一个方法&#xff0c;让应用程序检测哪些服务是可用的并探测这…

图解算法学习笔记(八):贪婪算法

目录 &#xff08;1&#xff09;背包问题 &#xff08;2&#xff09;集合覆盖问题 &#xff08;3&#xff09;NP完全问题 &#xff08;4&#xff09;小结 本章内容&#xff1a; 学习如何处理没有快速算法的问题&#xff08;NP完全问题&#xff09;。学习近似算法&#xff…

【CodeForces - 1152C 】Neko does Maths(数学数论,lcm,gcd性质)

题干&#xff1a; 给出a,b<1e9&#xff0c;你要找到最小的k使得lcm(ak,bk)尽可能小&#xff0c;如果有多个k给出同样的最小公倍数&#xff0c;输出最小的一个k。 解题报告&#xff1a; 因为题目中k太多了&#xff0c;先化简一下公式&#xff0c;假设a>b &#xff0c;则…

vs visual studio 2015安装后的几个问题

前言 最近在win7下重新安装了visual studio 2015&#xff0c;没有安装在默认路径下&#xff0c;编译时出现不少问题&#xff0c;整理如下 1.Failed to locate: "CL.exe". The system cannot find the file specified. TRACKER : error TRK0005: Failed to locate: “…

图解算法学习笔记(九):动态规划

目录 &#xff08;1&#xff09;背包问题 &#xff08;2&#xff09;最长公共子串 &#xff08;3&#xff09;小结 本章内容&#xff1a; 学习动态规划&#xff0c;它将问题分成小问题&#xff0c;并先着手解决这些小问题。学习如何设计问题的动态规划解决方案。 &#xff…

Java(win10安装jdk,第一个hello world)

Java 第一步 &#xff1a;安装jdk 推荐默认安装。&#xff08;安装到C盘&#xff09;第二步 &#xff1a;配置jdk环境 JAVA_HOME C:\Program Files\Java\jdk1.8.0_191 JDK的安装路径 Path&#xff1a; C:\Program Files\Java\jdk1.8.0_191\bin JDK下bin目录的路径 &#xf…

【POJ - 3177】Redundant Paths(边双连通分量,去重边)

题干&#xff1a; In order to get from one of the F (1 < F < 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being f…

#pragma 详解

#pragma 求助编辑 pragma - 必应词典美[prɡmə]英[prɡmə]n.〔计〕杂注网络编译指示&#xff1b;显示编译指示&#xff1b;特殊指令 百科名片 在所有的预处理指令中&#xff0c;#Pragma 指令可能是最复杂的了&#xff0c;它的作用是设定编译器的状态或者是指示编译器完成一些…

1.绪论

目录 &#xff08;1&#xff09;C语言传值与传地址变量 &#xff08;2&#xff09;算法效率的度量 &#xff08;3&#xff09;基本操作 &#xff08;4&#xff09;主函数 主要由实现基本操作和算法的程序构成。这些程序有6类&#xff1a; 数据存储结构&#xff0c;文件名第…

Java 新手习题()

循环 1.&#xff08;for 循环&#xff09;计算123…100 的和 package com.fxm.day03.test; public class day03test1{public static void main(String[] args){int sum 0;for(int i 1; i < 101; i){sum i;}System.out.println("1到100的和&#xff1a;"sum);…

(ECC)椭圆曲线加密算法原理和C++实现源码

目录 &#xff08;1&#xff09;ECC加密原理&#xff1a; &#xff08;2&#xff09;编译生成LibTommath静态库 &#xff08;3&#xff09;ECC源码 今天介绍一下利用LibTommath数学库实现椭圆曲线加密算法的原理和源码。 &#xff08;1&#xff09;ECC加密原理&#xff1a;…

【POJ - 2553】The Bottom of a Graph(tarjan强连通分量缩点,模板题)

题干&#xff1a; We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product VV, its elements being called edges. Then G(…

c语言3种链接属性: 外部(external), 内部(internal),无设置(none)

c语言中&#xff0c;多个文件组合的时候&#xff0c;有可能标示名相同&#xff0c;那么这个时候编译器如何判别的呢&#xff1f; c语言中有3种链接属性: 外部&#xff08;external&#xff1a;可以被其他文件访问到&#xff09;, 内部(internal&#xff1a;无法被其他文件访问到…

java 数组习题

2.写一个函数&#xff0c;返回一个整数数组的平均值 package com.fxm.day05.test; public class Day05test{public static void main(String[] args){int[] a1 {2,9,5,7,4};int n average(a1);System.out.println(n);}public static int average(int[] a){int sum 0;for(in…

机器学习笔记(1):Introduction

目录 1&#xff09;welcome 2&#xff09;What is Machine Learning 3&#xff09;Supervised Learning 4&#xff09;Unsupervised Learning 1&#xff09;welcome 第一个视频主要介绍了机器学习目前的案例&#xff0c;主要有&#xff1a;数据库挖掘、医疗记录、生物工程…

【POJ - 3352】Road Construction(Tarjan,边双连通分量)

题干&#xff1a; Its almost summer time, and that means that its almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads…

C++匿名命名空间

当定义一个命名空间时&#xff0c;可以忽略这个命名空间的名称&#xff1a; namespce {char c;int i;double d;}编译器在内部会为这个命名空间生成一个唯一的名字&#xff0c;而且还会为这个匿名的命名空间生成一条using指令。所以上面的代码在效果上等同于&#xff1a;namespa…