码迷,mamicode.com
首页 > 其他好文 > 详细

简单的整数分解模板

时间:2018-11-30 00:51:42      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:形式   temp   nbsp   for   red   color   简单的   分解   ++   

将整数分解为素因子

//将素因子全部分解出来
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;
}

 

简单的整数分解模板

标签:形式   temp   nbsp   for   red   color   简单的   分解   ++   

原文地址:https://www.cnblogs.com/033000-/p/10041664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!