codeforces 446E E. Divisors(数论+暴力)

题目链接:

codeforces 446E


题目大意:

给出一个x,k,每次操作都会将x分解因数,得到新的序列,然后每次再分解序列中的每一个数,按照每一个数分解因数从小到大排,整体顺序不做调整。


题目分析:

因为x是10^12以内的数,我们可以 nxlog(n) 105 个答案,就是对于每个因数递归的分解到1,然后输出答案,达到要求的答案数停止,或者不可以再分解了停止,第二种的复杂度是 O(nlog2n),O(105logn)


AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

typedef long long LL;

LL x,k;
vector<LL> ans,temp;

void init ( )
{
    ans.clear();
    temp.clear();
    for ( LL i = 1 ; i*i <= x ; i++ )
    {
        if ( x%i ) continue;
        temp.push_back ( i );
        LL y = x/i;
        if ( y == i ) continue;
        temp.push_back ( y );
    }
    sort ( temp.begin() , temp.end() );
}

void dfs ( LL x , LL n )
{
    if ( n == 0 || x == 1 )
    {
        ans.push_back( x );
        return;
    }
    for ( int i = 0 ; i < temp.size() ; i++ )
    {
        LL tt = temp[i];
        if ( tt > x ) break;
        //cout << tt << endl;
        if ( x%tt ) continue;
        dfs ( tt , n-1LL );
        if ( ans.size() >= 100000 ) return;
    }
}

int main ( )
{
    while ( ~scanf ( "%lld%lld" , &x , &k ) )
    {   
        init();
        //cout << temp.size() << endl;
        dfs ( x , k );  
        //cout << ans.size() << endl;
        int lim = 1e5;
        lim = min ( (int)ans.size() , lim );
        for ( int i = 0 ; i < lim ; i++ )
            printf ( "%lld " , ans[i] );
        puts ("");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值