简单的整数分解模板

将整数分解为素因子

//将素因子全部分解出来
template<class T> void Reduce(T x,T *p,T &tot){
    tot=0;
    for(T i=2;i*i<=x;i++){
        while(x%i==0) {
            x/=i;
            p[tot++]=i;
        }
    }
  if(x>1) p[tot++]=x; }

将整数分解为幂的形式

template<class T> void Reduce(T x,T *p,T &tot){
    tot=0;
    for(T i=2;i*i<=x;i++){
        T ret=1;
        while(x%i==0) {
            x/=i;
            ret*=i;
        }
        if(ret>1) p[tot++]=ret;
    }
    if(x>1) p[tot++]=x;
}

猜你喜欢

转载自www.cnblogs.com/033000-/p/10041664.html