题目链接

传送门

题面

思路

设\(x=\prod\limits_{i=l}^{r}a_i\)=\(\prod\limits_{i=1}^{n}p_i^{c_i}\)

由欧拉函数是积性函数得:

\[\begin{aligned}
\phi(x)&=\phi(\prod\limits_{i=1}^{n}p_i^{c_i})&\\
&=\prod\limits_{i=1}^{n}\phi(p_i^{c_i})&\\
&=\prod\limits_{i=1}^{n}p_i^{c_i}\times \frac{p_i-1}{p_i}&\\
&=x\times \prod\limits_{i=1}^{n}\frac{p_i-1}{p_i}&
\end{aligned}
\]

因此对于此题我们用线段树来维护区间乘积\(x\)和每个素数的是否存在。

由于小于等于\(300\)的素数只有\(62\)个,因此我们可以用一个\(long\) \(long\)变量来存,也可以用\(bitset\)写,第一次写这题的时候用的\(long\) \(long\)变量,这次暑训专题里面又遇到这个题目就用\(bitset\)写了一下当作是学习\(bitset\)的用法,发现\(bitset\)是真的省空间昂。

代码实现如下

\(long\) \(long\)变量写法

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 4e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; char op[15];
LL inv[305];
LL ans1, ans2;
int p[305], isp[63];
int n, q, m, l, r, x; LL qpow(LL x, int n) {
LL res = 1;
while(n) {
if(n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
} void init() {
for(int i = 2; i <= 300; i++) p[i] = 1;
for(int i = 2; i * i <= 300; i++) {
if(p[i]) {
for(int j = i * i; j <= 300; j += i) {
p[j] = 0;
}
}
}
for(int i = 2; i <= 300; i++) if(p[i]) isp[m++] = i;
for(int i = 0; i < 62; i++) inv[i] = qpow(isp[i], mod - 2);
} struct node {
int l, r;
LL lazy1, lazy2, sum, pp;
}segtree[maxn<<2]; void push_up(int rt) {
segtree[rt].sum = segtree[lson].sum * segtree[rson].sum % mod;
segtree[rt].pp = segtree[lson].pp | segtree[rson].pp;
} void push_down(int rt) {
LL x = segtree[rt].lazy1;
(segtree[lson].lazy1 *= x) %= mod;
(segtree[rson].lazy1 *= x) %= mod;
(segtree[lson].sum *= qpow(x, segtree[lson].r - segtree[lson].l + 1)) %= mod;
(segtree[rson].sum *= qpow(x, segtree[rson].r - segtree[rson].l + 1)) %= mod;
segtree[rt].lazy1 = 1;
x = segtree[rt].lazy2;
segtree[lson].lazy2 |= x;
segtree[rson].lazy2 |= x;
segtree[lson].pp |= x;
segtree[rson].pp |= x;
segtree[rt].lazy2 = 0;
} void build(int rt, int l, int r) {
segtree[rt].l = l, segtree[rt].r = r;
segtree[rt].sum = segtree[rt].pp = 0;
segtree[rt].lazy1 = 1, segtree[rt].lazy2 = 0;
if(l == r) {
scanf("%lld", &segtree[rt].sum);
for(int i = 0; i < 62; i++) {
if(segtree[rt].sum % isp[i] == 0) segtree[rt].pp |= (1LL<<i);
}
return;
}
int mid = (l + r) >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
push_up(rt);
} void update(int rt, int l, int r, int x) {
if(segtree[rt].l == l && segtree[rt].r == r) {
(segtree[rt].sum *= qpow(x, segtree[rt].r - segtree[rt].l + 1)) %= mod;
(segtree[rt].lazy1 *= x) %= mod;
for(int i = 0; i < 62; i++) {
if(x % isp[i] == 0) segtree[rt].pp |= (1LL<<i), segtree[rt].lazy2 |= (1LL<<i);
}
return;
}
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(r <= mid) update(lson, l, r, x);
else if(l > mid) update(rson, l, r, x);
else {
update(lson, l, mid, x);
update(rson, mid + 1, r, x);
}
push_up(rt);
} void query(int rt, int l, int r) {
if(segtree[rt].l == l && segtree[rt].r == r) {
(ans1 *= segtree[rt].sum) %= mod;
ans2 |= segtree[rt].pp;
return;
}
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(r <= mid) query(lson, l, r);
else if(l > mid) query(rson, l, r);
else {
query(lson, l, mid);
query(rson, mid + 1, r);
}
} int main(){
init();
scanf("%d%d", &n, &q);
build(1, 1, n);
while(q--) {
scanf("%s%d%d", op, &l, &r);
if(op[0] == 'T') {
ans1 = 1, ans2 = 0;
query(1, l, r);
for(int i = 0; i < 62; i++) {
if(ans2 & (1LL<<i)) {
ans1 = ans1 * (isp[i] - 1) % mod * inv[i] % mod;
}
}
printf("%lld\n", ans1);
} else {
scanf("%d", &x);
update(1, l, r, x);
}
}
return 0;
}

\(bitset\)写法

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 1000000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; char op[20];
bool v[305];
bitset<64> pp;
int cnt, n, q, l, r, x;
int p[65], inv[65]; int qpow(int x, int n) {
int res = 1;
while(n) {
if(n & 1) res = 1LL * res * x % mod;
x = 1LL * x * x % mod;
n >>= 1;
}
return res;
} void init() {
for(int i = 2; i <= 300; ++i) {
if(!v[i]) {
p[cnt++] = i;
}
for(int j = 0; j < cnt && i * p[j] <= 300; ++j) {
v[i*p[j]] = 1;
if(i % p[j] == 0) break;
}
}
for(int i = 0; i < cnt; ++i) inv[i] = qpow(p[i], mod - 2);
} struct node {
int l, r, mul, lazy1;
bitset<64> b, lazy2;
}segtree[maxn<<2]; void push_up(int rt) {
segtree[rt].mul = 1LL * segtree[lson].mul * segtree[rson].mul % mod;
segtree[rt].b = segtree[lson].b | segtree[rson].b;
} void push_down(int rt) {
segtree[lson].lazy1 = 1LL * segtree[lson].lazy1 * segtree[rt].lazy1 % mod;
segtree[rson].lazy1 = 1LL * segtree[rson].lazy1 * segtree[rt].lazy1 % mod;
segtree[lson].mul = 1LL * segtree[lson].mul * qpow(segtree[rt].lazy1, segtree[lson].r - segtree[lson].l + 1) % mod;
segtree[rson].mul = 1LL * segtree[rson].mul * qpow(segtree[rt].lazy1, segtree[rson].r - segtree[rson].l + 1) % mod;
segtree[rt].lazy1 = 1;
segtree[lson].b |= segtree[rt].lazy2;
segtree[rson].b |= segtree[rt].lazy2;
segtree[lson].lazy2 |= segtree[rt].lazy2;
segtree[rson].lazy2 |= segtree[rt].lazy2;
segtree[rt].lazy2.reset();
} void build(int rt, int l, int r) {
segtree[rt].l = l, segtree[rt].r = r;
segtree[rt].lazy1 = 1, segtree[rt].lazy2.reset();
segtree[rt].b.reset();
if(l == r) {
scanf("%d", &segtree[rt].mul);
int x = segtree[rt].mul;
for(int i = 0; i < cnt; ++i) {
if(x % p[i] == 0) segtree[rt].b.set(i);
}
return;
}
int mid = (l + r) >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
push_up(rt);
} void update(int rt, int l, int r, int x) {
if(segtree[rt].l == l && segtree[rt].r == r) {
segtree[rt].mul = 1LL * segtree[rt].mul * qpow(x, segtree[rt].r - segtree[rt].l + 1) % mod;
segtree[rt].lazy1 = 1LL * segtree[rt].lazy1 * x % mod;
for(int i = 0; i < cnt; ++i) {
if(x % p[i] == 0) segtree[rt].lazy2.set(i), segtree[rt].b.set(i);
}
return;
}
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(r <= mid) update(lson, l, r, x);
else if(l > mid) update(rson, l, r, x);
else {
update(lson, l, mid, x);
update(rson, mid + 1, r, x);
}
push_up(rt);
} int query(int rt, int l, int r) {
if(segtree[rt].l == l && segtree[rt].r == r) {
pp |= segtree[rt].b;
return segtree[rt].mul;
}
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(r <= mid) return query(lson, l, r);
else if(l > mid) return query(rson, l, r);
else return 1LL * query(lson, l, mid) * query(rson, mid + 1, r) % mod;
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
init();
scanf("%d%d", &n, &q);
build(1, 1, n);
while(q--) {
scanf("%s%d%d", op, &l, &r);
if(op[0] == 'M') {
scanf("%d", &x);
update(1, l, r, x);
} else {
pp.reset();
int ans = query(1, l, r);
for(int i = 0; i < cnt; ++i) {
if(pp[i]) ans = 1LL * ans * (p[i] - 1) % mod * inv[i] % mod;
}
printf("%d\n", ans);
}
}
return 0;
}

Please, another Queries on Array?(Codeforces Round #538 (Div. 2)F+线段树+欧拉函数+bitset)的更多相关文章

  1. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

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

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

  3. Please, another Queries on Array? CodeForces - 1114F (线段树,欧拉函数)

    这题刚开始看成求区间$\phi$和了........先说一下区间和的做法吧...... 就是说将题目的操作2改为求$(\sum\limits_{i=l}^{r}\phi(a[i]))\%P$ 首先要知 ...

  4. Codeforces Round #426 (Div. 2) D 线段树优化dp

    D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  5. Nastya Hasn't Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)

    题目链接 传送门 题面 题意 给你一个\(a\)数组和一个\(k\)数组,进行\(q\)次操作,操作分为两种: 将\(a_i\)增加\(x\),此时如果\(a_{i+1}<a_i+k_i\),那 ...

  6. Codeforces Round #538 (Div. 2)

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

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

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

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

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

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

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

随机推荐

  1. c#的BeginInvoke和EndInvoke使用demo

    BeginInvoke方法可以使用线程异步地执行委托所指向的方法.然后通过EndInvoke方法获得方法的返回值(EndInvoke方法的返回值就是被调用方法的返回值),或是确定方法已经被成功调用. ...

  2. go中值传递、引用传递、指针传递的区别

    go语言中的值类型: int.float.bool.array.sturct等 值传递是指在调用函数时将实际参数复制一份传递到函数中,这样在函数中如果对参数进行修改,将不会影响到实际参数 声明一个值类 ...

  3. 解决net core mvc 中文乱码问题

    在Startup 配置文件下的ConfigureServices方法中添加:    services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All ...

  4. 【插件】【idea】的Mybatis Plugin插件方便mapper接口方法和mapper XML文件之间来回切换

    效果 安装 这是2019.2版本的,旧版的有点不一样

  5. 移动APP接口安全性设计

    移动APP接口是怎么保证安全性的,可以采用https,或者是非对称加密. 接口加密的目的是防止被别人用抓包工具,抓包后篡改数据. 关于加密算法常见的有对称加密(DES)和非对称加密(RSA) 对称加密 ...

  6. [SourceTree] - 提交代码失败 "git -c diff.mnemonicprefix=false -c core.quotepath=false" 之解决

    背景 使用 SourceTree 提交代码失败,尝试了重装 SourceTree 和 Git 问题依旧. 错误信息 git -c diff.mnemonicprefix=false -c core.q ...

  7. IIS提速的几个优化

    一.内存池右键高级设置 1.设置队列5000 2.设置固定回收时间 3.设置空闲时间Suspend 二.网站右键高级设置 1.启用预加载

  8. LeetCode 5071. 找出所有行中最小公共元素(Java)

    题目:5071. 找出所有行中最小公共元素 给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1 ...

  9. 《学渣Linux笔记》——关于.bashrc与profile(涉及交互式与非交互式、登录与非登录shell)

    <学渣Linux笔记>--关于.bashrc与profile(涉及交互式与非交互式.登录与非登录shell) 1.基本概念(个人理解) 交互式shell:等待用户输入,并执行相应操作的sh ...

  10. C复习 (C premier plus和C和指针)