当前位置:网站首页>Luogu P4588: [TJOI2018]数学计算
Luogu P4588: [TJOI2018]数学计算
2022-08-05 08:11:00 【9ack!?】
题目大意
题目大意是这样对于一个数字x,有若干种操作:
- 操作1是对x乘上一个数字m
- 操作2是让x第pos次的操作失效
每次操作后输出当前x对一个数字取模的值。这个问题乍一看挺简单,但是由于取模的数字不一定是质数,所以逆元也不能用了,暴力做法不行我们就思考一些巧妙的方法。通过时间当作下标来把这些m组织起来,再用线段树维护所有段的乘积,操作1就是把当前时间对应的值置为m,操作2就是把当前时间对应的值置为1。
由于每次都需要精确递归到长度为1的段,所以标记也不需要打,直接开干。
代码
#include <cstdio>
#include <algorithm>
using namespace std;
#define lc k<<1
#define rc k<<1|1
typedef long long ll;
const int maxn = 1e5+5;
ll a[maxn], f[maxn*4];
int q, M;
void build(int k, int l, int r) {
f[k] = 1;
if(l == r) return;
int m = (l+r) >> 1;
build(lc, l, m);
build(rc, m+1, r);
}
inline void mul(int k, int l, int r, int t, int p) {
if(l == r) {
f[k] = p; return; }
int m = (l+r) >> 1;
if(t <= m) {
mul(lc, l, m, t, p);
} else
mul(rc, m+1, r, t, p);
f[k] = (f[lc]*f[rc])%M;
}
inline void div(int k, int l, int r, int t) {
if(l == r) {
f[k] = 1; return; }
int m = (l+r) >> 1;
if(t <= m) {
div(lc, l, m, t);
} else
div(rc, m+1, r, t);
f[k] = (f[lc]*f[rc])%M;
}
int main(void)
{
freopen("in.txt", "r", stdin);
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &q, &M);
build(1, 1, q);
int op, p;
for(int i = 1; i <= q; ++i) {
scanf("%d%d", &op, &p);
if(op == 1) {
mul(1, 1, q, i, p);
} else {
div(1, 1, q, p);
}
printf("%lld\n", f[1]%M);
}
}
return 0;
}
边栏推荐
- C语言制作-QQ聊天室
- spark集群部署(第三弹)
- Codeforce 8.1-8.7做题记录
- Embedded Systems: Basic Timers
- 512色色谱图
- XSS靶机通关以及XSS介绍
- Pagoda measurement - building small and medium-sized homestay hotel management source code
- Adb 授权过程分析
- 浅谈自动采集程序及入库
- [Repost] Marry a man must marry a man whose salary is at least 3571.4 yuan higher than yours
猜你喜欢

Ethernet Principle
![[Structural Internal Power Cultivation] Structural Realization Stages (2)](/img/eb/c80e12edbf4a411227be7e33096ed3.png)
[Structural Internal Power Cultivation] Structural Realization Stages (2)

Adb authorization process analysis

七夕看什么电影好?爬取电影评分并存入csv文件

v-if/v-else determines whether to display according to the calculation

餐饮大单品「真香」,却没有穿透周期的能力

How to make pictures clear in ps, self-study ps software photoshop2022, simple and fast use ps to make photos clearer and more textured

Chapter 12 贝叶斯网络

Pagoda measurement - building small and medium-sized homestay hotel management source code

unity 头发的渲染
随机推荐
ps怎么拼图,自学ps软件photoshop2022,PS制作拼图效果
原型&原型链
window.open 全屏展示
Fiddler工具讲解
[Structural Internal Power Cultivation] The Mystery of Enumeration and Union (3)
P1103 书本整理
Redis实现分布式锁-原理-问题详解
The color of life divine
浅谈自动采集程序及入库
routing----router
DataFrame insert row and column at specified position
The toss of MM before going to the street (interesting)
Three solutions to solve cross-domain in egg framework
Antdesign a-select 下拉框超出长度换行显示
复现一次循环和两次循环
餐饮大单品「真香」,却没有穿透周期的能力
Controlling number and letter input in ASP
Constellation ideal lover
RedisTemplate: error template not initialized; call afterPropertiesSet() before using it
软件系统测试和验收测试有什么联系与区别?专业软件测试方案推荐