Codeforces Round #538 (Div. 2) F. Please, another Queries on Array? 线段树 + 欧拉函数

传送门

文章目录

题意:

给你一个序列 a a a,你需要实现两种操作:

( 1 ) (1) (1) [ l , r ] [l,r] [l,r] a i a_i ai都乘 r r r

( 2 ) (2) (2) ϕ ( ∏ i = l r a i )   m o d   1 e 9 + 7 \phi(\prod_{i=l}^ra_i)\bmod 1e9+7 ϕ(i=lrai)mod1e9+7

1 ≤ n ≤ 4 e 5 , 1 ≤ 1 ≤ 2 e 5 , 1 ≤ a i , r ≤ 300 1\le n\le 4e5,1\le 1\le 2e5,1\le a_i,r\le 300 1n4e5,112e5,1ai,r300

思路:

注意到 a , r a,r a,r都很小, 300 300 300以内的质数最多有 60 60 60几个,在 l l ll ll的范围内,这就明示我们状压这几个质因子。

在考虑欧拉函数有个公式 ϕ ( x ) = x ∗ ∏ ( p − 1 p ) \phi(x)=x*\prod(\frac{p-1}{p}) ϕ(x)=x(pp1),其中 p p p x x x的质因子。

由于是求区间乘起来的欧拉函数,利用上面的式子,因为我们对 x x x取模了,所以需要将质因子状压成 s t a t e state state,其中 1 1 1代表有, 0 0 0代表没有,让后直接维护一下就行了,求一下区间 a a a的乘积以及区间内 s t a t e state state,最后乘上 p − 1 p \frac{p-1}{p} pp1即可。

注意需要提前状压成一个状态修改,如果对每个质因子修改会徒增 8 8 8的常数导致 T L E TLE TLE

由于用到了快速幂,复杂度 O ( n l o g 2 n ) O(nlog^2n) O(nlog2n)

如果求的欧拉函数是区间和,我们就不能直接按照上面那样求了,需要每个质因子分开来考虑贡献,如果一个区间内所有数都含有这个质因子,那么区间的欧拉函数直接乘上 p p p即可,否则需要递归子区间来判断。由于每个位置的每个质因子最多被添加一次,所以复杂度是可以得到保证的。最终递归到叶子的时候还是没有这个质因子,这个时候乘上 p − 1 p-1 p1即可。

复杂度也是 O ( n l o g 2 n ) O(nlog^2n) O(nlog2n)

// 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)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<bitset>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=400010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,m;
int a[N];
struct Node {
	int l,r;
	LL ans,s,lazy1,lazy2;
}tr[N<<2];
int mp[400],tot;
LL f[310];
vector<int>diver[310];

LL qmi(LL a,LL b) {
	LL ans=1;
	while(b) {
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans%mod;
}

void update(int u,LL x,LL y) {
	(tr[u].ans*=qmi(x,Len(u)))%=mod;
	tr[u].s|=y;
	(tr[u].lazy1*=x)%=mod;
	tr[u].lazy2|=y;
}

void pushup(int u) {
	tr[u].ans=(tr[L].ans*tr[R].ans)%mod;
	tr[u].s=tr[L].s|tr[R].s;
}

void pushdown(int u) {
	LL lazy1=tr[u].lazy1; tr[u].lazy1=1;
	LL lazy2=tr[u].lazy2; tr[u].lazy2=0;
	update(L,lazy1,lazy2); update(R,lazy1,lazy2);
}

void build(int u,int l,int r) {
	tr[u]={l,r};
	tr[u].lazy1=1;
	tr[u].lazy2=0;
	if(l==r) {
		tr[u].ans=1;		
		tr[u].s=0;
		return;
	}
	build(L,l,Mid); build(R,Mid+1,r);
	pushup(u);
}

void change(int u,int l,int r,LL x,LL y) {
	if(tr[u].l>=l&&tr[u].r<=r) {
		update(u,x,y);
		return;
	}
	pushdown(u);
	if(l<=Mid) change(L,l,r,x,y);
	if(r>Mid) change(R,l,r,x,y);
	pushup(u); 
}

Node query(int u,int l,int r) {
	if(tr[u].l>=l&&tr[u].r<=r) return tr[u];
	pushdown(u);
	if(r<=Mid) return query(L,l,r);
	if(l>Mid) return query(R,l,r);
	Node ans,ls=query(L,l,r),rs=query(R,l,r);
	ans.ans=ls.ans*rs.ans%mod;
	ans.s=ls.s|rs.s;
	return ans;
}

bool check(int x) {
	for(int i=2;i<=x/i;i++) if(x%i==0) return false;
	return true;
}
char s[20];

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	
	// cout<<400000*18*18*8<<endl;
	for(int i=2;i<=300;i++) if(check(i)) mp[i]=++tot,f[tot-1]=qmi(i,mod-2)*(i-1)%mod;
	scanf("%d%d",&n,&m);
	int mx=0;
	for(int i=2;i<=300;i++) {
		int x=i;
		for(int j=2;j<=x/j;j++) {
			while(x%j==0) diver[i].pb(j),x/=j;
		}
		if(x>1) diver[i].pb(x);
	}
	build(1,1,n);
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		LL state=0;
		for(auto x:diver[a[i]]) state|=1ll<<(mp[x]-1);
		change(1,i,i,a[i],state);
	}
	for(int i=1;i<=m;i++) {
		int l,r,x; 
		scanf("%s%d%d",s+1,&l,&r);
		if(s[1]=='M') {
			scanf("%d",&x);
			LL state=0;
			for(auto xx:diver[x]) state|=1ll<<(mp[xx]-1);
			change(1,l,r,x,state);
		} else {
			Node ans=query(1,l,r);
			for(int i=0;i<tot;i++) if(ans.s>>i&1) (ans.ans*=f[i])%=mod;
			printf("%lld\n",ans.ans);
		}
	}



	return 0;
}
/*
4e5*18*18*8
*/









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值