原题链接 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. SpringBoot 启动流程分析(寻找扩展点)

    1.SpringBoot maven 依赖版本 <?xml version="1.0" encoding="UTF-8"?> <project ...

  2. 《深入理解Java虚拟机》读书笔记:HotSpot虚拟机对象探秘

    基于实用优先的原则,以常用的虚拟机HotSpot和常用的内存区域Java堆为例,深入探讨HotSpot虚拟机在Java堆中对象分配.布局和访问的全过程.以下是本节内容的脑图. HotSpot虚拟机对象 ...

  3. Unity的AssetPostprocessor之Model:深入解析与实用案例 1

    Unity AssetPostprocessor模型相关函数详解 在Unity中,AssetPostprocessor是一个非常有用的工具,它可以在导入资源时自动执行一些操作.在本文中,我们将重点介绍 ...

  4. 【双系统】Win10/Win11 引导 Ubuntu

    目录 纲要 注意 写在最前 1. Win 分区 2. Ubuntu刻盘 3. 安装 Ubuntu 4. 配置引导 纲要 本文主要介绍了如何在已安装 Win10/Win11 前提下安装 Ubuntu 双 ...

  5. ATtiny88初体验(四):看门狗

    ATtiny88初体验(四):看门狗 ATtiny88单片机的看门狗使用内部独立的128KHz时钟源,拥有3种工作模式: Interrupt模式:超时产生中断: System Reset模式:超时产生 ...

  6. 错过这5大AI绘画提示词平台,你会拍大腿!别问,直接收藏!

    如今,AI绘画已经不再是简单的技术展示,而是逐渐转向了商业化的运营. 有的人利用AI生成的图片,再结合ChatGPT产生的文字,然后在平台上发布,这样就可以赚取平台的广告费. 其他一些变现操作参考之前 ...

  7. 《SQL与数据库基础》19. 日志

    目录 日志 错误日志 二进制日志 日志格式 日志查看 日志删除 查询日志 慢查询日志 本文以 MySQL 为例 日志 错误日志 错误日志是 MySQL 中最重要的日志之一,它记录了当 mysql 启动 ...

  8. 如何通过API接口获取京东的商品评论

    如果您想要获取京东的商品评论,可以通过API接口来实现.这篇文章会介绍如何使用京东API接口获取商品的评论数据. 首先,您需要到京东开放平台注册成为开发者,然后创建一个应用程序.通过这个应用程序,您可 ...

  9. Leetcode刷题笔记——二分法

    二分法是搜索算法中极其典型的方法,其要求输入序列有序并可随机访问.算法思想为 输入:有序数组nums,目的数值target 要求输出:如果target存在在数组中,则输出其index,否则输出-1 将 ...

  10. ViTPose+:迈向通用身体姿态估计的视觉Transformer基础模型

    身体姿态估计旨在识别出给定图像中人或者动物实例身体的关键点,除了典型的身体骨骼关键点,还可以包括手.脚.脸部等关键点,是计算机视觉领域的基本任务之一.目前,视觉transformer已经在识别.检测. ...