题目链接:
题目大意:
给出一个x,k,每次操作都会将x分解因数,得到新的序列,然后每次再分解序列中的每一个数,按照每一个数分解因数从小到大排,整体顺序不做调整。
题目分析:
因为x是10^12以内的数,我们可以 n√的预处理出x所有的因数,然后了解到其实每个因数如果一直分解深度是log(n)的复杂度,那么我们要得到这 105 个答案,就是对于每个因数递归的分解到1,然后输出答案,达到要求的答案数停止,或者不可以再分解了停止,第二种的复杂度是 O(n√⋅log2n),第一种的复杂度是O(105⋅logn)
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 ("");
}
}