[51nod 1203]JZPLCM
2016-12-12 22:24
141 查看
题目大意
求区间lcm。带模运算。做法
不知道有没有更简单的做法。最小公倍数是什么意思?一堆数的最小公倍数,就是对于每一个质数p,这些数每个数中p的次数的最大值k,然后最小公倍数就会包含一个p^k。
p很大时k会很小,p很小时k会很大,这是两个互相制约的因素,因此考虑使用两种算法然后进行平衡结合。
设置阈值B=230(差不多就是√n)
对于所有<=B的质数p都枚举一遍,然后计算出每个数包含多少个p,再对次数做ST表,每个询问可以直接询问区间最大值然后计算贡献。这部分的复杂度的话,因为只有根号n以内的质数,个数大概为√n/log n,ST的预处理需要n log n,一共n√n。
对于所有>B的质数,每个数最多只有1个,而且次数肯定为1,这意味着一个区间只要存在某个大质数那么贡献就为这个大质数。用b[i]表示第i个数包含的这个大质数(没有就是1),处理出往左往右第一个相同b值的位置,然后上莫队,左右端插入删除通过处理出的left和right来维护贡献。这部分的复杂度的话,逆元用预处理,那么过程就是n√n。
#include<cstdio> #include<algorithm> #include<cmath> #define fo(i,a,b) for(i=a;i<=b;i++) #define fd(i,a,b) for(i=a;i>=b;i--) using namespace std; typedef long long ll; const int maxn=50000+10,B=230,mo=1000000007; struct dong{ int l,r,id; } ask[maxn]; int a[maxn],c[maxn],f[maxn][20],pri[maxn],zjy[maxn],belong[maxn]; int ans[maxn],left[maxn],right[maxn],last[maxn],ni[maxn]; bool bz[maxn]; int i,j,k,l,r,t,n,m,top,now; int quicksortmi(int x,int y){ if (!y) return 1; int t=quicksortmi(x,y/2); t=(ll)t*t%mo; if (y%2) t=(ll)t*x%mo; return t; } int getmax(int l,int r){ int z=zjy[r-l+1]; return max(f[l][z],f[r-(1<<z)+1][z]); } bool cmp(dong a,dong b){ return belong[a.l]<belong[b.l]||belong[a.l]==belong[b.l]&&a.r<b.r; } int main(){ scanf("%d%d",&n,&m); fo(i,1,n) scanf("%d",&a[i]); fo(i,1,m){ scanf("%d%d",&ask[i].l,&ask[i].r); ask[i].id=i; ans[i]=1; } fo(i,2,B){ if (!bz[i]) pri[++top]=i; fo(j,1,top){ if (i*pri[j]>B) break; bz[i*pri[j]]=1; if (i%pri[j]==0) break; } } fo(i,1,n) zjy[i]=floor(log(i)/log(2)); fo(k,1,top){ t=pri[k]; fo(i,1,n) c[i]=0; fo(i,1,n){ while (a[i]%t==0){ c[i]++; a[i]/=t; } } fo(i,1,n) fo(j,0,zjy ) f[i][j]=0; fo(i,1,n) f[i][0]=c[i]; fo(j,1,zjy ) fo(i,1,n-(1<<j)+1) f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]); fo(i,1,m){ l=getmax(ask[i].l,ask[i].r); ans[i]=(ll)ans[i]*quicksortmi(t,l)%mo; } } fo(i,1,n) belong[i]=(i-1)/B+1; fo(i,1,n) last[i]=0; fo(i,1,n){ left[i]=last[a[i]]; last[a[i]]=i; } fo(i,1,n) last[i]=n+1; fd(i,n,1){ right[i]=last[a[i]]; last[a[i]]=i; } sort(ask+1,ask+m+1,cmp); l=1;r=0; now=1; fo(i,1,n) ni[i]=quicksortmi(i,mo-2); fo(i,1,m){ while (l<ask[i].l){ if (right[l]>r) now=(ll)now*ni[a[l]]%mo; l++; } while (l>ask[i].l){ l--; if (right[l]>r) now=(ll)now*a[l]%mo; } while (r>ask[i].r){ if (left[r]<l) now=(ll)now*ni[a[r]]%mo; r--; } while (r<ask[i].r){ r++; if (left[r]<l) now=(ll)now*a[r]%mo; } ans[ask[i].id]=(ll)ans[ask[i].id]*now%mo; } fo(i,1,m) printf("%d\n",ans[i]); }
相关文章推荐
- 51nod 1203 JZPLCM
- 51nod 1203 JZPLCM 莫队算法
- 51Nod 1203 JZPLCM
- 51Nod-1203-JZPLCM
- 51nod 1203 JZPLCM 离线 + 树状数组
- 51nod 1203 jzplcm
- 【51nod 1203】【JZPLCM】【莫队算法】
- 51 nod 1203 JZPLCM
- 【51 Nod 1203】JZPLCM
- 【TsinsenA1339】JZPLCM(顾昱洲) 树状数组
- [51nod1203]JZPLCM
- 51Nod1203 2012集训队答辩 JZPLCM
- J2CA0056I: 连接管理器接收到来自资源 jdbc/gywork 的资源适配器的致命连接错误。接收到的异常为 com.ibm.websphere.ce.cm.StaleConnectionException: JZ006: 捕获到 IO 例外:com
- Oracle PL/SQL入门之案例实践
- APEX:PL/SQL(匿名块)输出HTML
- 51Nod-1066-Bash游戏
- 51Nod-1293-球与切换器
- 51nod 1083 矩阵取数问题
- PL/SQL工具连接ORALCE数据库的方法
- oracle 11g PL/SQL Programming学习十三