原题链接 F. Please, another Queries on Array?

这道题让求\(\phi(\prod\limits_{i = l}^r a_i)\),然后我们化简一下。

设\(P\)是\(\prod\limits_{i = l}^r a_i\)质因子的集合,那么化简得:

\[\phi(\prod\limits_{i = l}^r a_i) = \prod\limits_{i = l}^r a_i \times \prod\limits_{p \in P} \cfrac{p-1}{p}
\]

因此我们分成两部分计算,前边部分\(\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?的更多相关文章

  1. Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树

    https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...

  2. 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}\) 由欧拉函数是积性函数得: \[ ...

  3. Codeforces Round #538 (Div. 2) (CF1114)

    Codeforces Round #538 (Div. 2) (CF1114)   今天昨天晚上的cf打的非常惨(仅代表淮中最低水平   先是一路缓慢地才A掉B,C,然后就开始杠D.于是写出了一个O( ...

  4. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  5. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  6. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  7. Codeforces Round #538 (Div. 2) (A-E题解)

    Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...

  8. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  9. 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries

    题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...

  10. Codeforces Round #538 (Div. 2)

    目录 Codeforces 1114 A.Got Any Grapes? B.Yet Another Array Partitioning Task C.Trailing Loves (or L'oe ...

随机推荐

  1. 20.1K Star!Notion的开源替代方案:AFFiNE

    Notion这款笔记软件相信很多开发者都比较熟悉了,很多读者,包括我自己都用它来记录和管理自己的笔记.今天给大家推荐一个最近比较火的开源替代方案:AFFiNE.目前该开源项目已经斩获20.1K Sta ...

  2. 关于Linux下服务器MySQL的安装和搭建

    一.检测是否已经安装Mysql 检测 # yum list installed | grep mysql //检查安装 # yum -y remove mysql-libs.x86_64 //卸载 / ...

  3. CF1601 题解

    偶然看这一场的题目,忽然很有感觉,于是写了一下 A 题面 考虑每一位可以单独分开考虑 考虑单独的一位,每次要选 \(m\) 个位置,可能产生贡献的位置就是这位为 1 的数,设数量为 \(x\),则 \ ...

  4. AcWing 第 92 场周赛 C题 4866. 最大数量 题解

    原题链接 链表 + 并查集乱搞做法: 思路 首先可以发现,想要让度数尽量大,那我们应该构造成菊花图,即下图所示: 对于每个需求,我们可以知道,如果之前他们没有连在一起,那我们一定得把他们连在一起,该过 ...

  5. cdn 引入的资源需要通过 externals 排除打包哦~

    cdn 指的是通过相互连接的网络系统,使用最靠近用户的服务器将音乐.图片等资源以高效率和低成本的方式将内容传递给用户. 在 webpack 中,我们可能会将引入的第三方资源会编译成单独的文件,作为静态 ...

  6. struct(C# 参考)

    struct 类型是一种值类型,通常用来封装小型相关变量组,例如,矩形的坐标或库存商品的特征. 下面的示例显示了一个简单的结构声明. 1 public struct Book 2 { 3 public ...

  7. typora使用教程&高级用法&Markdown

    typora使用教程&高级用法&Markdown typora介绍 哇啦哇啦哇啦哇,,,,,,,,,,,,, 提示:小白看不懂的话,建议哔哩哔哩搜索"遇见狂神说", ...

  8. 【Qt6】工具提示以及调色板设置

    工具提示即 Tool Tip,当用户把鼠标移动到某个UI对象上并悬停片刻,就会出现一个"短小精悍"的窗口,显示一些说明性文本.一般就是功能描述,让用户知道这个XX是干啥用的. 在 ...

  9. 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(3)--自定义用户控件

    在我们创建界面元素的时候,不管在Vue3+ElementPlus的前端上,还是Winform桌面端上,都是会利用自定义用户控件来快速重用一些自定义的界面内容,对自定义用户控件的封装处理,也是我们开发W ...

  10. C#集成ViewFaceCore人脸检测识别库

    前言 人脸检测与识别现在已经很成熟了,C# 上有 ViewFaceCore 这个很方便的库,但这种涉及到 native 调用的库,一般会有一些坑,本文记录一下开发和部署的过程. 本文的项目是 AIHu ...