Codeforces Round #256 (Div. 2) E. Divisors

E. 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), where a is a sequence of integers. Function f(a) returns the following sequence: first all divisors of a1 go in the increasing order, then all divisors of a2 go in the increasing order, and so on till the last element of sequence a. For example, f([2, 9, 1]) = [1, 2, 1, 3, 9, 1].

Let's determine the sequence Xi, for integer i (i ≥ 0)X0 = [X] ([X] is a sequence consisting of a single number X), 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 first 105 elements of this sequence.

Input

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

Output

Print the elements of the sequence Xk in a single line, separated by a space. If the number of elements exceeds 105, then print only the first 105 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 


题意:给一个数x,然后分出因子算1次。然后把因子再分又算一次,问k次后有输出。

题解:先把x因子都分解出来,存下来,再用DFS从第一个 因子开始搜索,直到这个等于1或者搜索到第k层输出这个数,或者输出的个数超过1e5则return。

#include <bits/stdc++.h>
using namespace std;
#define ll __int64
ll a[100000],t,cnt;
void DFS(ll n,ll k)
{
	if (cnt>=1e5)
		return;
	if (k==0 || n==1)
	{
		cnt++;
		printf("%I64d ",n);
		return;
	}
	for (ll i=0;i<t && n>=a[i];i++)
		if (n % a[i]==0)
			DFS(a[i],k-1);
	return;
}
int main()
{
	t=0;
	ll n,k;
	scanf("%I64d%I64d",&n,&k);
	for (ll i=1;i*i<=n;i++)
		if (n % i==0)
		{
			a[t++]=i;
			if (i!=n/i)
			a[t++]=n/i;
		}
	cnt=0;
	sort(a,a+t);
	DFS(n,k);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值