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 ...
随机推荐
- MySQL(二) decimal数据默认处理
create table decimal_test(id int auto_increment PRIMARY key,score decimal(5,2) -- 取值范围是 -999.99 到 99 ...
- 彻底理解JavaScript中的prototype、__proto__
虽然在JavaScript里一切皆对象,但为了理解原型链系统,我们需要将JavaScript的对象分为对象和函数两大类.在此基础上,JavaScript的原型链逻辑遵从以下通用规则: 对象有__pro ...
- SrpingBoot入门到入坟04-配置文件
SpringBoot使用一个全局的配置文件,名称是固定,作用就是修改SpringBoot自动配置的默认值. *application.properties *application.yml 先看看两者 ...
- VC++操作注册表(创建,读取,更改,删除)
#include "stdafx.h" #include <Windows.h> #include <iostream> using namespace s ...
- golang中switch用法细节
1. switch穿透-fallthrough, 如果在case语句块后增加fallthrough,则会继续执行下一个case,也叫switch穿透,默认只穿透一层 2. Type Switch: s ...
- Vue路由系统
Vue路由系统 一切分离都是为了更好的结合,本文详细介绍了前后端分离架构之后,前端如何实现路由控制,即Vue路由系统. 一.VueRouter实现原理 VueRouter的实现原理是根据监控锚点值的改 ...
- Java中数据存储分配
(1)内存分配的策略 按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈式的,和堆式的. 静态存储分配是指在编译时就能确定每个数据目标在运行时刻的存储空间需求,因而在编 译时就可以给 ...
- 【C#】上机实验一
1.开发一个控制台应用程序,根据提示从键盘获取一个华氏温度,请转换并输出对应的摄氏温度. using System; namespace Project { class Program { publi ...
- asp.net core-9.依赖注入的使用
http://www.jessetalk.cn/2017/11/06/di-in-aspnetcore/
- List 集合 使用 remove 踩得坑
不要在 foreach 循环里进行元素的 remove/add 操作.remove 元素请使用 Iterator方式,如果并发操作,需要对 Iterator 对象加锁. 正确例子: Iterato ...