标签:stdin mem for class 等于 模型 data 数据结构 center
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> #include <cstring> #include <queue> #include <complex> #include <stack> #define LL long long int #define dob double #define FILE "JZPLCM" using namespace std; const int N = 50010; const int Mod = 1e9+7; struct Node{int to,next;}E[N]; int n,m,P[N],vis[N],tot,S[N],Ny[N],block,A[20][N],bel[N],bin[N],B[51][N]; int Log[N],Pw[51][N],Ans[N],res=1,head[N],Tot; struct Data{ int l,r,id; bool operator <(const Data &d)const{ if(bel[l]==bel[d.l])return r<d.r; return bel[l]<bel[d.l]; } }Ask[N]; inline int gi(){ int x=0,res=1;char ch=getchar(); while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)res*=-1;ch=getchar();} while(ch<=‘9‘&&ch>=‘0‘)x=x*10+ch-48,ch=getchar(); return x*res; } inline void link(int u,int v){ E[++tot]=(Node){v,head[u]}; head[u]=tot; } inline void prepare(){ for(int i=2;i<N;++i){ if(!vis[i])P[++tot]=i; for(int j=1;j<=tot;++j){ int ytk=i*P[j];if(ytk>=N)break; vis[ytk]=1;if(i%P[j]==0)break; } } Ny[1]=1; for(int i=2;i<N;++i) Ny[i]=1ll*(Mod-Mod/i)*Ny[Mod%i]%Mod; } inline void Insert(int x){ for(int e=head[x];e;e=E[e].next){ int y=E[e].to;bin[y]++; if(bin[y]==1)res=1ll*res*y%Mod; } } inline void Delete(int x){ for(int e=head[x];e;e=E[e].next){ int y=E[e].to;bin[y]--; if(bin[y]==0)res=1ll*res*Ny[y]%Mod; } } inline void Modui(){ memset(bin,0,sizeof(bin)); Insert(S[1]);int l=1,r=1; for(int i=1;i<=m;++i){ if(r<Ask[i].r) for(int j=r+1;j<=Ask[i].r;++j) Insert(S[j]); if(r>Ask[i].r) for(int j=r;j>Ask[i].r;--j) Delete(S[j]); if(l<Ask[i].l) for(int j=l;j<Ask[i].l;++j) Delete(S[j]); if(l>Ask[i].l) for(int j=l-1;j>=Ask[i].l;--j) Insert(S[j]); l=Ask[i].l;r=Ask[i].r; Ans[Ask[i].id]=1ll*Ans[Ask[i].id]*res%Mod; } } int main() { freopen(FILE".in","r",stdin); freopen(FILE".out","w",stdout); prepare();n=gi();m=gi();block=sqrt(n); bin[0]=1;for(int i=1;i<=16;++i)bin[i]=bin[i-1]*2; for(int i=1,j=0;i<=n;i*=2,j++)Log[i]=j; for(int i=1;i<=N;++i) if(!Log[i])Log[i]=Log[i-1]; for(int i=1;i<=n;++i)S[i]=gi(),bel[i]=i/block+1; for(int j=1;j<=n;++j){ int ytk=S[j]; for(int i=1;i<=50;++i){ int num=0; while(ytk%P[i]==0)num++,ytk/=P[i]; B[i][j]=num; if(ytk==1)break; } if(ytk>1)link(S[j],ytk); } for(int i=1;i<=50;++i){ Pw[i][0]=1; for(int j=1;j<=19;++j) Pw[i][j]=1ll*Pw[i][j-1]*P[i]%Mod; } for(int i=1;i<=m;++i){ int l=gi(),r=gi();Ans[i]=1; Ask[i]=(Data){l,r,i}; } for(int k=1;k<=50;++k){ for(int i=1;i<=n;++i)A[0][i]=B[k][i]; for(int i=1;i<=16;++i) for(int j=1;j<=n;++j) A[i][j]=max(A[i-1][j],A[i-1][min(n,j+bin[i-1])]); for(int i=1;i<=m;++i){ int l=Ask[i].l,r=Ask[i].r,dt=r-l+1,Lg=Log[dt]; int Mx=max(A[Lg][l],A[Lg][r-bin[Lg]+1]); Ans[i]=1ll*Ans[i]*Pw[k][Mx]%Mod; } } sort(Ask+1,Ask+m+1);Modui(); for(int i=1;i<=m;++i) printf("%d\n",Ans[i]); return 0; }
然后是对于51Nod的数据很多地方跑不过原数据的树状数组。
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> #include <cstring> #include <queue> #include <complex> #include <stack> #define LL long long int #define dob double #define FILE "JZPLCM" #define c233 printf("\n"); using namespace std; const int N = 50010; const int M = N*50; const int Mod = 1e9+7; struct Data{ int r,tr,kind,id; bool operator <(const Data &d)const{ return tr<d.tr; } }opt[N*40]; struct Pair{ int pre,id,val; Pair(){}; Pair(int p,int i,int v){pre=p;id=i;val=v;} bool operator <(const Pair &p)const{ return pre<p.pre; } }Y[M]; int n,m,P[N],tot,vis[N],A[N],cnt,ED[N],pre[M],bin[N],top,Ans[N],W[M],V[M]; struct BIT{ int T[M]; inline void clear(){ for(int i=0;i<=cnt;++i)T[i]=1; } inline int lb(int k){ return k&-k; } inline void update(int x,int val){ for(;x<=cnt;x+=lb(x))T[x]=1ll*T[x]*val%Mod; } inline int query(int x,int res=1){ for(;x>0;x-=lb(x))res=1ll*res*T[x]%Mod; return res; } }T; inline int gi(){ int x=0,res=1;char ch=getchar(); while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)res*=-1;ch=getchar();} while(ch<=‘9‘&&ch>=‘0‘)x=x*10+ch-48,ch=getchar(); return x*res; } inline void prepare(){ for(int i=2;i<N;++i){ if(!vis[i])P[++tot]=i; for(int j=1,ytk;j<=tot;++j){ ytk=i*P[j];if(ytk>=N)break; vis[ytk]=1;if(i%P[j]==0)break; } } } inline int QPow(int d,int z,int res=1){ for(;z;z>>=1,d=1ll*d*d%Mod) if(z&1)res=1ll*res*d%Mod; return res; } inline void solve(){ int i=1,j=1;T.clear(); while(i<=cnt && j<=top){ if(Y[i].pre<=opt[j].tr) T.update(Y[i].id,Y[i].val),++i; else{ int id=opt[j].id; if(opt[j].kind==1)Ans[id]=1ll*Ans[id]*T.query(opt[j].r)%Mod; else Ans[id]=1ll*Ans[id]*QPow(T.query(opt[j].r),Mod-2)%Mod; ++j; } } while(j<=top){ int id=opt[j].id; if(opt[j].kind==1)Ans[id]=1ll*Ans[id]*T.query(opt[j].r)%Mod; else Ans[id]=1ll*Ans[id]*QPow(T.query(opt[j].r),Mod-2)%Mod; ++j; } } int main() { freopen(FILE".in","r",stdin); freopen(FILE".out","w",stdout); n=gi();m=gi();prepare(); for(int i=1;i<=n;++i)A[i]=gi(); for(int i=1;i<=n;++i){ int ytk=A[i]; for(int j=1;P[j]<=ytk/P[j];++j) if(ytk%P[j]==0){ W[++cnt]=P[j];ytk/=P[j];V[cnt]=P[j]; while(ytk%P[j]==0) ++cnt,W[cnt]=W[cnt-1]*P[j],V[cnt]=P[j],ytk/=P[j]; } if(ytk>1)W[++cnt]=ytk,V[cnt]=ytk; ED[i]=cnt; } for(int i=1;i<=cnt;++i)pre[i]=bin[W[i]],bin[W[i]]=i; for(int i=1;i<=cnt;++i)Y[i]=Pair(pre[i],i,V[i]); sort(Y+1,Y+cnt+1); for(int i=1;i<=m;++i){ int l=gi(),r=gi();Ans[i]=1; opt[++top]=(Data){ED[r],ED[l-1],1,i}; opt[++top]=(Data){ED[l-1],ED[l-1],-1,i}; } sort(opt+1,opt+top+1);solve(); for(int i=1;i<=m;++i)printf("%d\n",Ans[i]); return 0; }
标签:stdin mem for class 等于 模型 data 数据结构 center
原文地址:http://www.cnblogs.com/fenghaoran/p/7612593.html