Divisors
Bizon the Champion isn't just friendly, he also is a rigorous coder.
Let's define function f(a), wherea is a sequence of integers. Function f(a) returns the following sequence: first all divisors ofa1 go in the increasing order, then all divisors ofa2 go in the increasing order, and so on till the last element of sequencea. For example, f([2, 9, 1]) = [1, 2, 1, 3, 9, 1].
Let's determine the sequence Xi, for integeri (i ≥ 0):X0 = [X] ([X] is a sequence consisting of a single numberX), Xi = f(Xi - 1)(i > 0). For example, at X = 6 we get X0 = [6],X1 = [1, 2, 3, 6],X2 = [1, 1, 2, 1, 3, 1, 2, 3, 6].
Given the numbers X and k, find the sequence Xk. As the answer can be rather large, find only the first105 elements of this sequence.
A single line contains two space-separated integers — X(1 ≤ X ≤ 1012) andk (0 ≤ k ≤ 1018).
Print the elements of the sequence Xk in a single line, separated by a space. If the number of elements exceeds105, then print only the first105 elements.
6 1
1 2 3 6
4 2
1 1 2 1 2 4
10 3
1 1 1 2 1 1 5 1 1 2 1 5 1 2 5 10
dfs题目,意思是说找一个数的因数(预处理)
x0就是本身
x1就是因数排列
x2因数再去分解
需要注意的就是1无论多少层都是1所以不用dfs下去
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
typedef long long LL;
const int M=100000;
LL ans[M+5];
LL fa[M]; //预处理的因子(因子的因子也在其中)
int nf;
int tot;
int fac(LL n)
{
int p=0;
for(int i=1;i<=sqrt(n);i++)
{
if(n%i==0)
{
fa[p++]=i;
if(n!=(LL)i*i)
fa[p++]=n/i;
}
}
sort(fa,fa+p);
return p;
}
void dfs(LL n,LL k)
{
if(tot>=M)
return;
if(k==0||n==1)
{
ans[tot++]=n;
return;
}
for(int i=0;i<nf&&fa[i]<=n;i++)
{
if(tot>=M)
return;
if(n%fa[i]==0)
dfs(fa[i],k-1);
}
}int main()
{
LL n,k;
while(scanf("%I64d%I64d",&n,&k)!=EOF)
{
if(n==1)
{
printf("1\n");
continue;
}
if(k>=M)
{
for(int i=0;i<n-1;i++) //这里错写一次M-1还超时一次,注意细节
printf("1 ");
printf("1\n");
continue;
}
nf=fac(n); //因子的个数
tot=0;
dfs(n,k);
for(int i=0;i<tot-1;i++)
printf("%I64d ",ans[i]);
printf("%I64d\n",ans[tot-1]);
}
return 0;
}