将整数分解为素因子
- // 将素因子全部分解出来
- 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;
- }
来源: http://www.bubuko.com/infodetail-2866562.html