这个分解质因数啊,分来分去的真难弄
算是较好的分解质因数的方法了吧(雾)
#include<iostream> #include<string> #include<cstring> #include<algorithm> #include<cstdio> #include<queue> #include<vector> #define MAXX 10000000 using namespace std; int stdx[100000]; int lin[300000]; int qq[100000]; int tail2=0; int zhishu[10000000]; int q[1000000]; int a[1000000]; int tail=0; //priority_queue<int,vector<int>,greater<int> >s; int top=3; int m1,m2; void before1()//预处理质数; { zhishu[1]=2; zhishu[2]=3; zhishu[3]=5; for(int i=2;i<=15000;i++) { int flag=0; for(int u=2;u*u<=i;u++) { if(i%u==0){flag=1;break;} } if(flag==0)zhishu[++top]=i; } } int before2(int x)//求出m { for(int i=1;i<=top;i++) { if(x%zhishu[i]==0){if(stdx[zhishu[i]]==0)q[++tail]=zhishu[i];stdx[zhishu[i]]+=m2;x/=zhishu[i];i=0;} if(zhishu[i]>x)break; } return 0; } int check(int x) { for(int i=1;i<=top;i++) { if(x%zhishu[i]==0) { if(lin[zhishu[i]]==0) qq[++tail2]=zhishu[i]; lin[zhishu[i]]++; x/=zhishu[i]; i=0; } if(zhishu[i]>x)break; } if(tail2<tail)return MAXX; int maxx=0; for(int i=1;i<=tail;i++) { int x=q[i]; if(lin[x]==0)return MAXX; if(stdx[x]%lin[x]!=0)maxx=max(maxx,stdx[x]/lin[x]+1); else maxx=max(maxx,stdx[x]/lin[x]); } return maxx; } void chuli() { for(int i=1;i<=tail2;i++) { lin[q[i]]=0; qq[i]=0; } return ; } int main() { int n; int ans=MAXX; cin>>n; before1(); cin>>m1>>m2; before2(m1); for(int i=1;i<=n;i++) { cin>>a[i]; ans=min(check(a[i]),ans); chuli(); } if(ans==MAXX) cout<<-1<<endl; else cout<<ans<<endl; return 0; }