原题链接 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. EaselJS 源码分析系列--第四篇

    鼠标交互事件 前几篇关注的是如何渲染,那么鼠标交互如何实现呢? Canvas context 本身没有像浏览器 DOM 一样的交互事件 EaselJS 如何在 canvas 内实现自己的鼠标事件系统? ...

  2. 王道oj/problem12(动态申请内存存储数组)

    网址:http://oj.lgwenda.com/problem/12 思路:用输入的整型创建对应数组,用scanf消除换行键: 用gets()输入语句并输出,再释放. 代码: #define _CR ...

  3. Linq开发技巧与业务逻辑校验

    Linq 是一种基于 .NET Framework 的编程语言,它的出现极大地提高了开发效率.Linq 提供了一种统一的查询语法,使得开发人员可以使用一种语言来查询不同类型的数据源,包括对象.集合.数 ...

  4. 个人GAN训练的性能迭代

    使用GAN进行生成图片 损失函数的迭代 DCGAN->Wasserstein GAN-> Wasserstein GAN + Gradient Penalty Discriminator训 ...

  5. 【Azure Batch】在批处理的Task中如何让它执行多个CMD指令呢

    问题描述 根据Azure Batch的入门文档(使用 Azure 门户创建 Batch 帐户并运行作业 : https://docs.azure.cn/zh-cn/batch/quick-create ...

  6. CAP项目集成带身份和证书验证的MongoDB

    大家好,我是Edison. 最近,在使用CAP事件总线时,碰到了这样一个需求:微服务采用的是MongoDB,而且还是带身份验证 和 SSL根证书验证的.由于目前网上能找到的资料,都是不带身份验证的Mo ...

  7. Strimzi Kafka Bridge(桥接)实战之三:自制sdk(golang版本)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<Strimzi Kafka B ...

  8. 第一个 Go 程序"hello,world" 与 main 函数和Go常用基本命令

    第一个 Go 程序"hello,world" 与 main 函数和Go常用基本命令 目录 第一个 Go 程序"hello,world" 与 main 函数和Go ...

  9. Lab3 存储过程与触发器

    实验三 存储过程与触发器  实验目的: 学习SQL语言进行编程的基本方法与技术,能够编写存储过程.触发器解决数据库需要处理的复杂问题.  实验内容: 1. 设计一个存储过程或者自定义函数,练习存储 ...

  10. DB22

    IBM官方网站提供了DB2 Express-C版本的软件免费下载: 下载地址 : http://www.ibm.com/developerworks/cn/downloads/im/udbexp/