Codeforces Round #539 (Div. 1) E - Sasha and a Very Easy Test 线段树
如果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 线段树的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树
题解看这里 liouzhou_101的博客园 更简洁的代码看这里: #include <bits/stdc++.h> using namespace std; typedef long l ...
- 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 ...
- Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)
思路: dfs序其实是很水的东西. 和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...
- Codeforces Round #539 (Div. 2)
Codeforces Round #539 (Div. 2) A - Sasha and His Trip #include<bits/stdc++.h> #include<iost ...
随机推荐
- Verilog整理
1.两种实例化 2.运算符//逻辑运算+按位运算//拼接运算符 3.reg默认为1位 4.{16{1}}与{16{1'b1}}不同 5.[1023:0] in ha[3:0]=(in>>( ...
- 图像人脸检测+人眼检测 (opencv + c++)
摘要:实现图像中人脸检测,和人眼定位.输出检测标记图像和定位坐标. 工具:vs2015 opencv3 C++ 资源:haarcascade_frontalface_alt2.xml;haarcas ...
- WUSTOJ 1285: Factors(Java)
1285: Factors 参考 hadis_fukan的博客--wustoj 1285 Factors 题目 输入一个数n,找出1~n之间(包括1,n)的质因子最多的数(x)的质因子个数(f ...
- Python开发【第三章】:编码转换
一.字符编码与转码 1.bytes和str 之前有学过关于bytes和str之间的转换,详细资料->bytes和str(第四字符串) 2.为什么要进行编码和转码 由于每个国家电脑的字符编码格式不 ...
- WPF入门(4)——资源
引用<深入浅出WPF>对资源的解读: 每个WPF的界面元素都具有一个名为Resources的属性,这个属性继承自FrameworkElement类,其类型为ResourceDictiona ...
- 浅谈人脸识别中的loss 损失函数
浅谈人脸识别中的loss 损失函数 2019-04-17 17:57:33 liguiyuan112 阅读数 641更多 分类专栏: AI 人脸识别 版权声明:本文为博主原创文章,遵循CC 4.0 ...
- JVM 介绍
JVM 介绍: JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的 ...
- 15-MySQL DBA笔记-运维管理
第15章 运维管理 随着各种技术的快速发展,现今的DBA可以比以前的DBA维护多得多的数据库实例.DBA已经越来越像一个资源的管理者,而不是简单的操作步骤执行人.本章将为读者介绍规模化运维之道.首先, ...
- Deep Learning方向的paper
转载 http://hi.baidu.com/chb_seaok/item/6307c0d0363170e73cc2cb65 个人阅读的Deep Learning方向的paper整理,分了几部分吧,但 ...
- liteide
/liteide$ bin/liteide Cannot mix incompatible Qt library (version 0x40806) with this library (versio ...