Codeforces Round #538 (Div. 2) F. Please, another Queries on Array? 线段树 + 欧拉函数
傳送門
文章目錄
- 題意:
- 思路:
題意:
給你一個(gè)序列aaa,你需要實(shí)現(xiàn)兩種操作:
(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以內(nèi)的質(zhì)數(shù)最多有606060幾個(gè),在llllll的范圍內(nèi),這就明示我們狀壓這幾個(gè)質(zhì)因子。
在考慮歐拉函數(shù)有個(gè)公式?(x)=x?∏(p?1p)\phi(x)=x*\prod(\frac{p-1}{p})?(x)=x?∏(pp?1?),其中ppp是xxx的質(zhì)因子。
由于是求區(qū)間乘起來的歐拉函數(shù),利用上面的式子,因?yàn)槲覀儗?duì)xxx取模了,所以需要將質(zhì)因子狀壓成statestatestate,其中111代表有,000代表沒有,讓后直接維護(hù)一下就行了,求一下區(qū)間aaa的乘積以及區(qū)間內(nèi)statestatestate,最后乘上p?1p\frac{p-1}{p}pp?1?即可。
注意需要提前狀壓成一個(gè)狀態(tài)修改,如果對(duì)每個(gè)質(zhì)因子修改會(huì)徒增888的常數(shù)導(dǎo)致TLETLETLE。
由于用到了快速冪,復(fù)雜度O(nlog2n)O(nlog^2n)O(nlog2n)。
如果求的歐拉函數(shù)是區(qū)間和,我們就不能直接按照上面那樣求了,需要每個(gè)質(zhì)因子分開來考慮貢獻(xiàn),如果一個(gè)區(qū)間內(nèi)所有數(shù)都含有這個(gè)質(zhì)因子,那么區(qū)間的歐拉函數(shù)直接乘上ppp即可,否則需要遞歸子區(qū)間來判斷。由于每個(gè)位置的每個(gè)質(zhì)因子最多被添加一次,所以復(fù)雜度是可以得到保證的。最終遞歸到葉子的時(shí)候還是沒有這個(gè)質(zhì)因子,這個(gè)時(shí)候乘上p?1p-1p?1即可。
復(fù)雜度也是O(nlog2n)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 */總結(jié)
以上是生活随笔為你收集整理的Codeforces Round #538 (Div. 2) F. Please, another Queries on Array? 线段树 + 欧拉函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dnf90级驱剑士/剑宗/剑皇太刀流刷图
- 下一篇: P3804 【模板】后缀自动机 (SAM