当前位置:网站首页>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;
}
边栏推荐
- Spark cluster deployment (third bullet)
- How to make pictures clear in ps, self-study ps software photoshop2022, simple and fast use ps to make photos clearer and more textured
- 路由----router
- ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色
- 吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
- 彩绘漂亮MM集
- iptables实现网络限制下ntp自定义端口同步时间
- Codeforce 8.1-8.7做题记录
- 让硬盘更快,让系统更稳定
- 力扣刷题八月第一天
猜你喜欢
随机推荐
双向循环带头链表
长期招聘嵌入式开发-深圳宝安
Codeforce 8.1-8.7做题记录
MongoDB 语法大全
浅谈自动采集程序及入库
Thinking after writing a code with a very high CPU usage
Basic introduction of stack and queue and C language implementation of functions such as creation, destruction, entry and exit, counting the number of elements, viewing elements, etc., as well as stac
DNS 查询原理详解
创业者如何吸引风险投资商
行走社会100绝招
VXE-Table融合多语言
别把你的天使弄丢了
Controlling number and letter input in ASP
存储过程编写经验和优化措施
漂亮MM和普通MM的区别
DataFrame在指定位置插入行和列
love is a sad song
v-if/v-else根据计算判断是否显示
青苹果论坛重新开放
php向mysql写入数据失败









