如果mod是质数就好做了,但是做除法的时候对于合数mod可能没有逆元。所以就只有存一下mod的每个质因数(最多9个)的幂,和剩下一坨与mod互质的一部分。然后就能做了。有点恶心。

CODE

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 100005;
const int MAXP = 9;
int n, q, a[MAXN], p[MAXP], cnt, mod, phi;
inline int qpow(int a, int b) {
int re=1;
for(;b;a=1ll*a*a%mod,b>>=1)if(b&1)re=1ll*re*a%mod;
return re;
}
struct node {
int ind[MAXP];
node () { memset(ind, 0, sizeof ind); }
inline int init(int x) {
memset(ind, 0, sizeof ind);
for(int i = 0; i < cnt; ++i)
while(x % p[i] == 0)
x /= p[i], ind[i]++;
return x;
}
inline void operator *=(const node &o) {
for(int i = 0; i < cnt; ++i)
ind[i] += o.ind[i];
}
inline void operator /=(const node &o) {
for(int i = 0; i < cnt; ++i)
ind[i] -= o.ind[i];
}
inline int calc() {
int re = 1;
for(int i = 0; i < cnt; ++i)
re = 1ll * re * qpow(p[i], ind[i]) % mod;
return re;
}
}tag[MAXN<<2]; //tag存mod的质因数的幂
int res[MAXN<<2], rlz[MAXN<<2]; //res存与mod互质的部分的积
int sum[MAXN<<2], lz[MAXN<<2]; //sum存答案 inline void pre(int x) {
phi = x;
for(int i = 2; i*i <= x; ++i)
if(x % i == 0) {
p[cnt++] = i, phi = phi / i * (i-1);
while(x % i == 0) x /= i;
}
if(x > 1) p[cnt++] = x, phi = phi / x * (x-1);
} inline void upd(int i) { sum[i] = (sum[i<<1] + sum[i<<1|1]) % mod; }
inline void pd(int i) {
tag[i<<1] *= tag[i], tag[i<<1|1] *= tag[i];
memset(tag[i].ind, 0, sizeof tag[i].ind);
if(lz[i] != 1) {
sum[i<<1] = 1ll * sum[i<<1] * lz[i] % mod;
lz[i<<1] = 1ll * lz[i<<1] * lz[i] % mod;
sum[i<<1|1] = 1ll * sum[i<<1|1] * lz[i] % mod;
lz[i<<1|1] = 1ll * lz[i<<1|1] * lz[i] % mod;
lz[i] = 1;
}
if(rlz[i] != 1) {
res[i<<1] = 1ll * res[i<<1] * rlz[i] % mod;
rlz[i<<1] = 1ll * rlz[i<<1] * rlz[i] % mod;
res[i<<1|1] = 1ll * res[i<<1|1] * rlz[i] % mod;
rlz[i<<1|1] = 1ll * rlz[i<<1|1] * rlz[i] % mod;
rlz[i] = 1;
}
} void build(int i, int l, int r) {
//printf("SSS (%d,%d,%d)\n", i, l, r);
lz[i] = rlz[i] = 1;
if(l == r) {
int x; scanf("%d", &x);
res[i] = tag[i].init(x) % mod;
sum[i] = x % mod;
return;
}
int mid = (l + r) >> 1;
build(i<<1, l, mid);
build(i<<1|1, mid+1, r);
upd(i);
} void multi(int i, int l, int r, int x, int y, int num, node v, int val) {
if(x <= l && r <= y) {
tag[i] *= v;
lz[i] = 1ll * lz[i] * val % mod;
sum[i] = 1ll * sum[i] * val % mod;
rlz[i] = 1ll * rlz[i] * num % mod;
res[i] = 1ll * res[i] * num % mod;
return;
}
pd(i);
int mid = (l + r) >> 1;
if(x <= mid) multi(i<<1, l, mid, x, y, num, v, val);
if(y > mid) multi(i<<1|1, mid+1, r, x, y, num, v, val);
upd(i);
} void divide(int i, int l, int r, int x, int num, node v) {
if(l == r) {
sum[i] = res[i] = 1ll * res[i] * qpow(num, phi-1) % mod;
tag[i] /= v; sum[i] = 1ll * sum[i] * tag[i].calc() % mod;
return;
}
pd(i);
int mid = (l + r) >> 1;
if(x <= mid) divide(i<<1, l, mid, x, num, v);
else divide(i<<1|1, mid+1, r, x, num, v);
upd(i);
} inline int query(int i, int l, int r, int x, int y) {
if(x <= l && r <= y) return sum[i];
int mid = (l + r) >> 1, re = 0; pd(i);
if(x <= mid) re = (re + query(i<<1, l, mid, x, y)) % mod;
if(mid < y) re = (re + query(i<<1|1, mid+1, r, x, y)) % mod;
return re;
} int main() {
scanf("%d%d", &n, &mod); pre(mod);
build(1, 1, n);
scanf("%d", &q);
int op, l, r, x;
int num; node tmp;
while(q--) {
scanf("%d", &op);
if(op == 1) {
scanf("%d%d%d", &l, &r, &x);
num = tmp.init(x);
multi(1, 1, n, l, r, num, tmp, x);
}
else if(op == 2) {
scanf("%d%d", &l, &x);
num = tmp.init(x);
divide(1, 1, n, l, num, tmp);
}
else {
scanf("%d%d", &l, &r);
printf("%d\n", query(1, 1, n, l, r));
}
}
}

Codeforces Round #539 (Div. 1) E - Sasha and a Very Easy Test 线段树的更多相关文章

  1. Codeforces Round #373 (Div. 2) E. Sasha and Array 矩阵快速幂+线段树

    E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces Round #539 (Div. 2) - D. Sasha and One More Name(思维)

    Problem   Codeforces Round #539 (Div. 2) - D. Sasha and One More Name Time Limit: 1000 mSec Problem ...

  3. Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax(思维题)

    Problem   Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax Time Limit: 2000 mSec Problem ...

  4. Codeforces Round #539 (Div. 2) C. Sasha and a Bit of Relax(前缀异或和)

    转载自:https://blog.csdn.net/Charles_Zaqdt/article/details/87522917 题目链接:https://codeforces.com/contest ...

  5. Codeforces Round #539 (Div. 2) C Sasha and a Bit of Relax

    题中意思显而易见,即求满足al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar且l到r的区间长为偶数的这样的数对(l,r)的个数. 若al⊕al+1⊕…⊕amid=amid+1⊕amid ...

  6. Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树

    题解看这里 liouzhou_101的博客园 更简洁的代码看这里: #include <bits/stdc++.h> using namespace std; typedef long l ...

  7. Codeforces Round #539 (Div. 1) 1109F. Sasha and Algorithm of Silence's Sounds LCT+线段树 (two pointers)

    题解请看 Felix-Lee的CSDN博客 写的很好,不过最后不用判断最小值是不是1,因为[i,i]只有一个点,一定满足条件,最小值一定是1. CODE 写完就A,刺激. #include <b ...

  8. Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)

    思路: dfs序其实是很水的东西.  和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...

  9. Codeforces Round #539 (Div. 2)

    Codeforces Round #539 (Div. 2) A - Sasha and His Trip #include<bits/stdc++.h> #include<iost ...

随机推荐

  1. [转帖]MMU内存管理单元

    MMU内存管理单元 https://www.cnblogs.com/alantu2018/p/9002309.html 之前对这一块一直不理解 最近学习了点 CPU time slice 以及 con ...

  2. Python基础 第5章 条件、循环及其他语句(1)

    1. print和import 1.1 打印多个参数 可用 + 连接多个字符串,可保证被连接字符串前无空格: 可用sep=“_”,自定义各种分隔符: print("I"," ...

  3. dg环境连接ORA-00604,ORA-16000: database open for read-only access

    报错信息 根据客户提供的报错信息, ORA-: error occurred at recursive SQL level ORA-: database open for read-only acce ...

  4. SpringCloud Hystrix/Feign 整合 Hystrix 后首次请求失败解决方案

  5. JS原型的动态性

    由于在原型中查找成员的过程是一次搜索,所以我们对原型对象所做的任何修改都能立即从实例上反映出来(但不包括对原型对象的重写,下面会介绍到),即使是对原型的修改操作在创建实例之后.如下面的示例所示: fu ...

  6. Keras 训练 inceptionV3 并移植到OpenCV4.0 in C++

    1. 训练 # --coding:utf--- import os import sys import glob import argparse import matplotlib.pyplot as ...

  7. opencv 加载pb

    1.错误1         Tensor's data type is not supported the type of Mul  is DF_Float 2.  错误2 type == " ...

  8. React中setState如何修改深层对象?

    在React中经常会使用到setState,因为在react生态中,state就是一切.在开发过程中,时长会在state中遇到一些比较复杂的数据结构,类似下面这样的: 这时需要我们修改list中obj ...

  9. SMTP命令

    SMTP(Simple Mail Transfer Protocol)简单邮件传输协议 Basic Commands: HELO(Hello):标识用户身份 MAIL FROM:发件人地址 RCPT ...

  10. 嵌套的页面——自适应高度与跨越操作DOM

    <div id="myIframeId"> <iframe ref="myIframe" name="odpIframeName&q ...