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}\)
由欧拉函数是积性函数得:
\[\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;
}
版权声明:本文允许转载,转载时请注明原博客链接,谢谢~