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. 结构体buf_chunk_t

    /** Buffer pool chunk comprising buf_block_t */ typedef struct buf_chunk_struct buf_chunk_t; /** A c ...

  2. Json.net 同过 JsonConverter 调整导出值(未完成)

    public class TimeSpanConverter : JsonConverter { public override bool CanConvert(Type objectType) { ...

  3. UVa 442 (栈) Matrix Chain Multiplication

    题意: 给出一个矩阵表达式,计算总的乘法次数. 分析: 基本的数学知识:一个m×n的矩阵A和n×s的矩阵B,计算AB的乘法次数为m×n×s.只有A的列数和B的行数相等时,两个矩阵才能进行乘法运算. 表 ...

  4. CSS基础深入之细说盒子模型

    Html任何一个元素(element)都可以当成一个盒子(box)来看待,可以结合现实中的盒子来理解下文,下文其中一些单词应该是通俗易懂的需要记录的单词. 基本情况 每一个盒子都有一个内容区域(con ...

  5. HAOI2007反素数

    1053: [HAOI2007]反素数ant Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1346  Solved: 732[Submit][Sta ...

  6. Vim Vundle 插件管理器

    /********************************************************************** * Vim Vundle 插件管理器 * 说明: * 话 ...

  7. 让Codeigniter控制器支持多级目录

    <?php if (!defined('BASEPATH')) { exit ('No direct script access allowed');} class MY_Router exte ...

  8. 【转】【iOS】导航栏那些事儿

    原文网址:http://www.jianshu.com/p/f797793d683f 参考文章 navigationItem UINavigationItem UINavigationBar UIBa ...

  9. C# 中的值类型和引用类型

    原文 C# 中的值类型和引用类型 值类型(value type):int,long,float,double,decimal,char,bool 和 struct 统称为值类型.值类型变量声明后,不管 ...

  10. Swift不可变数组

    Objective-C编写了2个不同的类来区分不可变数组(NSArray)和可变数组(NSMutableArray): Swift通过使用常量和变量来区分不可变数组和可变数组. 只要将数组定义为常量, ...