CF_448E_Divisors

Divisors

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

A single line contains two space-separated integers — X(1 ≤ X ≤ 1012) andk (0 ≤ k ≤ 1018).

Output

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.

Sample test(s)
Input
6 1
Output
1 2 3 6 
Input
4 2
Output
1 1 2 1 2 4 
Input
10 3
Output
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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值