Codeforces 797A k-Factorization

A. k-Factorization
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Given a positive integer n, find k integers (not necessary distinct) such that all these integers are strictly greater than 1, and their product is equal to n.

Input

The first line contains two integers n and k (2 ≤ n ≤ 1000001 ≤ k ≤ 20).

Output

If it's impossible to find the representation of n as a product of k numbers, print -1.

Otherwise, print k integers in any order. Their product must be equal to n. If there are multiple answers, print any of them.

Examples
input
100000 2
output
2 50000 
input
100000 20
output
-1
input
1024 5
output
2 64 2 2 2 

虽然这道题很水,但是我不会写.....

题目大意:给你一个n,分解为k个数,这k个数可以相同,相乘要等于n

根据唯一分解定理,将n分解为质因数的形式

#include<iostream>
#include<vector>
using namespace std;
vector<int> vec;
void fun(int n){
	for(int i=2;i*i<=n;i++){
		while(n%i==0){
			vec.push_back(i);
			n/=i;
		}
	}
	if(n!=1) vec.push_back(n);
}
int main(){
	int n,k;
	while(cin>>n>>k){
		vec.clear();
		fun(n);
		if(vec.size()<k) cout<<-1<<endl;
		else{
			for(int i=0;i<k-1;i++){
				cout<<vec[i]<<' ';
			}
			int ans=1;
			for(int i=k-1;i<vec.size();i++){
				ans*=vec[i];
			}
			if(ans>1){
				cout<<ans<<endl;
			}
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值