Codeforces Round #538 (Div. 2) F. Please, another Queries on Array?
原题链接 F. Please, another Queries on Array?
这道题让求\(\phi(\prod\limits_{i = l}^r a_i)\),然后我们化简一下。
设\(P\)是\(\prod\limits_{i = l}^r a_i\)质因子的集合,那么化简得:
\]
因此我们分成两部分计算,前边部分\(\prod\limits_{i = l}^r a_i\)很容易维护,后边部分
\(\prod\limits_{p \in P} \cfrac{p-1}{p}\),我们知道一个数的欧拉函数只与有几个质因子有关,而\(300\)以内的质数只有\(62\)个,所有我们每次修改的时候只需要暴力枚举\(62\)个质数,然后状压存储即可。
// Problem: F. Please, another Queries on Array?
// Contest: Codeforces - Codeforces Round #538 (Div. 2)
// URL: https://codeforces.com/contest/1114/problem/F
// Memory Limit: 256 MB
// Time Limit: 5500 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 4E5 + 10;
const LL Mod = 1e9 + 7;
int a[N];
int primes[310], cnt = 0, st[310];
char op[10];
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i]) primes[cnt++] = i;
for (int j = 0; primes[j] <= n / i; j++) {
st[primes[j] * i] = 1;
if (i % primes[j] == 0) break;
}
}
}
struct SegMentTree {
int l, r;
LL products, mul, OR, tag;
}tr[N * 4];
LL qmi(LL a, LL b, LL p) {
LL res = 1ll;
while (b) {
if (b & 1) res = res * a % p;
b >>= 1;
a = a * a % p;
}
return res;
}
void pushup(int u) {
tr[u].products = tr[u << 1].products * tr[u << 1 | 1].products % Mod;
tr[u].OR = tr[u << 1].OR | tr[u << 1 | 1].OR;
}
void pushdown(int u) {
if (tr[u].mul == 1 || !tr[u].tag) return;
auto &root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1];
left.mul = left.mul * root.mul % Mod; right.mul = right.mul * root.mul % Mod;
left.products = left.products * qmi(root.mul, left.r - left.l + 1, Mod) % Mod;
right.products = right.products * qmi(root.mul, right.r - right.l + 1, Mod) % Mod;
left.tag = left.tag | root.tag; right.tag = right.tag | root.tag;
left.OR = left.OR | root.tag; right.OR = right.OR | root.tag;
root.mul = 1; root.tag = 0;
}
void build(int u, int l, int r) {
if (l == r) {
tr[u] = {l, r, 1ll * a[l], 1ll};
for (int i = 0; i < cnt; i++) {
if (a[l] % primes[i] == 0) {
tr[u].OR |= (1ll << i);
}
}
} else {
tr[u].l = l, tr[u].r = r, tr[u].mul = 1ll;
//tr[u] = {l, r};
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
void modify(int u, int l, int r, LL p, int x) {
if (l <= tr[u].l && tr[u].r <= r) {
tr[u].mul = tr[u].mul * 1ll * x % Mod;
tr[u].products = tr[u].products * qmi(1ll * x, 1ll * (tr[u].r - tr[u].l + 1), Mod) % Mod;
tr[u].OR |= p, tr[u].tag |= p;
return;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) modify(u << 1, l, r, p, x);
if (r > mid) modify(u << 1 | 1, l, r, p, x);
pushup(u);
}
LL ask(int u, int l, int r) {
if (l <= tr[u].l && tr[u].r <= r) {
return tr[u].products;
}
pushdown(u);
LL res = 1ll;
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) res = res * ask(u << 1, l, r) % Mod;
if (r > mid) res = res * ask(u << 1 | 1, l, r) % Mod;
return res;
}
int ask_flag(int u, int l, int r, int i) {
if (l <= tr[u].l && tr[u].r <= r) {
return (tr[u].OR >> i & 1);
}
pushdown(u);
LL res = 0ll;
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) res += ask_flag(u << 1, l, r, i);
if (r > mid) res += ask_flag(u << 1 | 1, l, r, i);
return res;
}
int main() {
get_primes(300);
//cout << cnt << endl;
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
build(1, 1, n);
while (q--) {
int l, r, x;
//cin >> s;
scanf("%s%d%d", &op, &l, &r);
if (op[0] == 'M') {
scanf("%d", &x);
LL p = 0;
for (int i = 0; i < cnt; i++) {
if (x % primes[i] == 0) {
p |= (1ll << i);
}
}
modify(1, l, r, p, x);
} else if (op[0] == 'T') {
LL res = ask(1, l, r);
for (int i = 0; i < cnt; i++) {
int p = primes[i];
if (ask_flag(1, l, r, i)) {
res = res * (p - 1) % Mod;
res = res * qmi(p, Mod - 2, Mod) % Mod;
}
}
printf("%lld\n", res);
}
}
return 0;
}
Codeforces Round #538 (Div. 2) F. Please, another Queries on Array?的更多相关文章
- Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树
https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...
- Please, another Queries on Array?(Codeforces Round #538 (Div. 2)F+线段树+欧拉函数+bitset)
题目链接 传送门 题面 思路 设\(x=\prod\limits_{i=l}^{r}a_i\)=\(\prod\limits_{i=1}^{n}p_i^{c_i}\) 由欧拉函数是积性函数得: \[ ...
- Codeforces Round #538 (Div. 2) (CF1114)
Codeforces Round #538 (Div. 2) (CF1114) 今天昨天晚上的cf打的非常惨(仅代表淮中最低水平 先是一路缓慢地才A掉B,C,然后就开始杠D.于是写出了一个O( ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #538 (Div. 2) (A-E题解)
Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries
题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...
- Codeforces Round #538 (Div. 2)
目录 Codeforces 1114 A.Got Any Grapes? B.Yet Another Array Partitioning Task C.Trailing Loves (or L'oe ...
随机推荐
- 文心一言 VS 讯飞星火 VS chatgpt (66)-- 算法导论6.5 5题
五.试分析在使用下列循环不变量时,HEAP-INCREASE-KEY 的正确性:在算法的第4~6行 while循环每次迭代开始的时候,子数组 A[1..A.heap-size]要满足最大堆的性质.如果 ...
- 《深入理解Java虚拟机》读书笔记:Class类文件的结构
Class类文件的结构 Sun公司以及其他虚拟机提供商发布了许多可以运行在各种不同平台上的虚拟机,这些虚拟机都可以载入和执行同一种平台无关的的程序存储格式--字节码(ByteCode),从而实现了程序 ...
- ABP Framework 7.4 RC 新增功能简介:增强微服务架构支持
ABP Framework 版本号:7.4.0-rc.1 发布时间:2023.8.16 阅读原文:ABP.IO Platform 7.4 RC Has Been Published 翻译:iEricL ...
- Pytest+Jenkins 学习笔记
Pytest+Jenkins 学习笔记 在软件测试工作中,单元测试通常是由开发人员执行的.针对最小单元粒度的组件测试,在完成了单元粒度的测试任务之后,通常就需要交由专职的测试人员将这些单元级的组件放到 ...
- Go 上下文的理解与使用
为什么需要 context 在 Go 程序中,特别是并发情况下,由于超时.取消等而引发的异常操作,往往需要及时的释放相应资源,正确的关闭 goroutine.防止协程不退出而导致内存泄露.如果没有 c ...
- 《Linux基础》01. 概述
@ 目录 1:Linux的应用领域 1.1:个人桌面领域的应用 1.2:服务器领域 1.3:嵌入式领域 2:Linux介绍 3:Linux和Unix的关系 4:Linux基本规则 Linux介绍 1: ...
- Python 基础面试第四弹
1. Python中常用的库有哪些,作用分别是什么 requests: requests 是一个用于发送 HTTP 请求的库,它提供了简单而优雅的 API,可以轻松地发送 GET.POST.PUT.D ...
- 【matplotlib基础】--绘图配置
Matplotlib 提供了大量配置参数,这些参数可以但不限于让我们从整体上调整通过 Matplotlib 绘制的图形样式,这里面的参数还有很多是功能性的,和其他工具结合时需要用的配置. 通过plt. ...
- 【matplotlib 实战】--平行坐标系
平行坐标系是一种统计图表,它包含多个垂直平行的坐标轴,每个轴表示一个字段,并用刻度标明范围.通过在每个轴上找到数据点的落点,并将它们连接起来形成折线,可以很容易地展示多维数据.随着数据增多,折线会堆叠 ...
- 前端三件套系例之JS——JavaScript内置方法
文章目录 1.Number 1-1 属性 1-2 方法 2.String 2-1 属性 2-2 方法 2-3 代码 3Array 3-1 创建数组 3-2 数组特点 3-3 数组的遍历(迭代) 34 ...