当前位置:网站首页>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;
}
边栏推荐
- 在ASP控制数字及字母输入
- SQL SERVER on master-slave table trigger design
- ps怎么拼图,自学ps软件photoshop2022,PS制作拼图效果
- Embedded Systems: Basic Timers
- 七夕看什么电影好?爬取电影评分并存入csv文件
- 导出SQLServer数据到Excel中
- Version number naming convention
- Pagoda measurement - building small and medium-sized homestay hotel management source code
- MongoDB 语法大全
- uniapp time component encapsulates year-month-day-hour-minute-second
猜你喜欢

D2--FPGA SPI接口通信2022-08-03

谷歌零碎笔记之MVCC(草稿)

MVCC of Google's Fragmented Notes (Draft)

SVG星球大战样式Toggle切换开关按钮

利用Jenkins的持续集成

MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)

MySQL database error The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)

Redis缓存以及存在的问题--缓存穿透、缓存雪崩、缓存击穿及解决方法

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

【结构体内功修炼】枚举和联合的奥秘(三)
随机推荐
SVG Star Wars Style Toggle Toggle Button
Qt writes custom controls: one of the text spotlight effects
Three solutions to solve cross-domain in egg framework
导出SQLServer数据到Excel中
手机上流行的各类谜语
Adb 授权过程分析
Spark cluster deployment (third bullet)
Controlling number and letter input in ASP
Antdesign a-select 下拉框超出长度换行显示
Access Denied: "microsoft.web.ui.webcontrols" workaround
漂亮MM和普通MM的区别
D2--FPGA SPI interface communication2022-08-03
pnpm 是凭什么对 npm 和 yarn 降维打击的
What is the connection and difference between software system testing and acceptance testing? Professional software testing solution recommendation
How to make a puzzle in PS, self-study PS software photoshop2022, PS make a puzzle effect
[Structure internal power practice] Structure memory alignment (1)
Constellation ideal lover
【结构体内功修炼】枚举和联合的奥秘(三)
Stored procedure writing experience and optimization measures
链表专项之环形链表