hdu1299 枚举因子

这题搞法很多,写个枚举因子的模版放在这里,另有用处。

typedef  long long  LL;

const   int  maxn = 10008  ;
bool    is[maxn] ;
int     ps ;
int     prime[maxn] ;

void    make(){
        memset(is , 0 , sizeof(is)) ;
        ps = 0 ;
        for(int i = 2 ; i < maxn ; i++){
            if(! is[i]) prime[ps++] = i ;
            for(int j = 0 ; j < ps && i * prime[j] < maxn ; j++){
                 is[i * prime[j]] = 1 ;
                 if(i % prime[j] == 0)  break ;
            }
        }
}

vector< pair<int , int> > plis ;
void   get_pfactor(LL n){
       plis.clear()  ;
       for(int i = 0 ; i < ps && prime[i] * prime[i] <= n ; i++){
            if(n % prime[i] == 0){
                 int c = 0 ;
                 while(n % prime[i] == 0){
                       c++ ;
                       n /= prime[i] ;
                 }
                 plis.push_back(make_pair(prime[i] , c * 2)) ;
            }
       }
       if(n != 1)  plis.push_back(make_pair(n , 2)) ;
}

LL     Pow(LL x , LL y){
       LL s = 1 ;
       for(; y ; y >>= 1){
            if(y & 1)  s *= x ;
            x *= x ;
       }
       return s ;
}

vector<LL> factor ;
void   dfs(LL f , int id){
       if(id >= plis.size()){
             factor.push_back(f) ;
             return ;
       }
       for(int i = 0 ; i <= plis[id].second ; i++){
             dfs(f * Pow(plis[id].first , i) , id+1) ;
       }
}

LL     ans(LL n){
       get_pfactor(n) ;
       factor.clear() ;
       dfs(1 , 0) ;
       sort(factor.begin() , factor.end()) ;
       LL  t = 0  ;
       for(int i = 0 ; i < factor.size() ; i++){
            LL x = factor[i] ;
            LL y = n  * n / factor[i] ;
            if(x <= y)  t++ ;
       }
       return t ;
}

int    main(){
       make() ;
       LL n  ;  int t , T = 1   ;
       cin>>t ;
       while(t--){
            cin>>n ;
            printf("Scenario #%d:\n" , T++) ;
            cout<< ans(n) << endl ;
            puts("")  ;
       }
       return 0 ;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值