首页 技术 正文
技术 2022年11月17日
0 收藏 663 点赞 2,595 浏览 2908 个字

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,那么我求逆元就好了

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

那就线段树咯……

HDU 5475 An easy problem 线段树

代码:

#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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,110
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,921