An easy problem

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5475

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

HINT

题意

一开始ans = 1

有两个操作

1.乘以x

2.除以第y个加入的数

然后需要mod

题解:

显然,不用mod的话,就是傻逼题了

但是有一个mod,那么我求逆元就好了

但是很蛋疼的是,有些数并没有逆元怎么办?

那就线段树咯……

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1) using namespace std;
const int maxn = 1e5 + ;
int Q;
long long MOD,X,C[maxn]; typedef long long SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum ;
void updata(SgTreeDataType v)
{
sum = v;
}
}; treenode tree[maxn*];
inline void push_up(int o)
{
tree[o].sum = (tree[*o].sum * tree[*o+].sum)%MOD;
} inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum = 1LL;
if (R > L)
{
int mid = (L+R) >> ;
build_tree(L,mid,o*);
build_tree(mid+,R,o*+);
}
} inline void updata(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].updata(v);
else
{
int mid = (L+R)>>;
if (QL <= mid) updata(QL,QR,v,o*);
if (QR > mid) updata(QL,QR,v,o*+);
push_up(o);
}
} inline SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
int mid = (L+R)>>;
SgTreeDataType res = 1LL;
if (QL <= mid) res *= query(QL,QR,*o);
if(res >= MOD) res %= MOD;
if (QR > mid) res *= query(QL,QR,*o+);
if(res >= MOD) res %= MOD;
return res;
}
} void initiaiton()
{
X=;
scanf("%d%I64d",&Q,&MOD);
build_tree(,Q,);
} void solve()
{
for(int i = ; i <= Q ; ++ i)
{
int type ;
long long y;
scanf("%d%I64d",&type,&y);
if(type == )
{
updata(i , i , y , );
printf("%I64d\n",query(,Q,));
}
else
{
updata(y , y , , );
long long cx = query(,Q,);
printf("%I64d\n",cx);
X = cx;
}
}
} int main(int argc,char *argv[])
{
int Case;
scanf("%d",&Case);
int cas=;
while(Case--)
{
initiaiton();
printf("Case #%d:\n",cas++);
solve();
}
return ;
}

HDU 5475 An easy problem 线段树的更多相关文章

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

    题意就不说了 思路:线段树,维护区间乘积.2操作就将要除的点更新为1. #include<iostream> #include<cstdio> #include<cstr ...

  2. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  3. hud-5475 An easy problem(线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  4. hdu 5443 The Water Problem 线段树

    The Water Problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  5. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  6. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  7. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  8. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

  9. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

随机推荐

  1. 理解Java对象序列化(二)

    关于Java序列化的文章早已是汗牛充栋了,本文是对我个人过往学习,理解及应用Java序列化的一个总结.此文内容涉及Java序列化的基本原理,以及多种方法对序列化形式进行定制.在撰写本文时,既参考了Th ...

  2. 函数page_get_space_id

    #define FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID  34 /****************************************************** ...

  3. Android LayoutInflater&LayoutInflaterCompat源码解析

    本文分析版本: Android API 23,v4基于 23.2.1 1 简介 实例化布局的XML文件成相应的View对象.它不能被直接使用,应该使用getLayoutInflater()或getSy ...

  4. 设计模式:Observer(观察者)—— Guava EventBus

    本文分为三个部分:   Observer(观察者) Guava EventBus详解 Guava EventBus使用示例   1. Observer(观察者)   1.1 背景   我们设计系统时, ...

  5. (八)学习CSS之line-style-type属性

    参考:http://www.w3school.com.cn/cssref/pr_list-style-type.asp 设置不同的列表样式: ul.circle {list-style-type:ci ...

  6. 无法找到.exe的调试信息

    原文:无法找到.exe的调试信息 前天重装了电脑,并配置了Visual Studio2005的VC正则库,boost/regex,运行速度马上快了三倍吧,到底是怎么快的,我还真说不清楚,因为电脑这玩意 ...

  7. tdx api z

    调用TdxAPI.dll函数 .DLL命令 TdxInit, 逻辑型, "TdxApi.dll", "TdxInit", , 初始化通达信实例,成功时返回tru ...

  8. codeforces 672C - Recycling Bottles 贪心水题

    感觉很简单,就是讨论一下 #include <stdio.h> #include <string.h> #include <algorithm> #include ...

  9. Oracle函数:求两个数的最大公约数

    CREATE OR REPLACE FUNCTION GETGYS(NUM1 NUMBER, NUM2 NUMBER) RETURN NUMBER IS RESULTNUM NUMBER; NUM3 ...

  10. css 横向渐变 图片阴影效果 字体模糊效果

    (必须加在Table的TD里,如果TD有背景就会遮盖阴影,可以把背景放到外套的表格里去) 背景颜色渐变 横向渐变 style="filter:progid:DXImageTransform. ...