1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Codeforces Round #538 (Div. 2) F. Please another Queries on Array? 线段树 + 欧拉函数

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

时间:2020-07-24 03:35:47

相关推荐

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

传送门

文章目录

题意:思路:

题意:

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

(1)(1)(1) 将[l,r][l,r][l,r]的aia_iai​都乘rrr。

(2)(2)(2) 求ϕ(∏i=lrai)mod1e9+7\phi(\prod_{i=l}^ra_i)\bmod 1e9+7ϕ(∏i=lr​ai​)mod1e9+7

1≤n≤4e5,1≤1≤2e5,1≤ai,r≤3001\le n\le 4e5,1\le 1\le 2e5,1\le a_i,r\le 3001≤n≤4e5,1≤1≤2e5,1≤ai​,r≤300

思路:

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

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

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

注意需要提前状压成一个状态修改,如果对每个质因子修改会徒增888的常数导致TLETLETLE。

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

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

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

// Problem: F. Please, another Queries on Array?// Contest: Codeforces - Codeforces Round #538 (Div. 2)// URL: /contest/1114/problem/F// Memory Limit: 256 MB// Time Limit: 5500 ms// // Powered by CP Editor ()//#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*/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。