Codeforces 797A - k-Factorization
2024-10-11 22:55:55
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 ≤ 100000, 1 ≤ 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
5 1
output
5
input
5 2
output
-1
input
1024 5
output
2 64 2 2 2
题目大意:输入两个数n和k,找k个大于1的整数,使他们的乘积为n,输出这k个数(如果有多个,输出任意一组),如果没有,输出-1。
方法:把n分解成质因数的乘积,并把这些质因数保存在数组里。如果质因数的个数c小于k,输出-1;否则,输出前k-1个质因数和k到c个质因数的乘积(这样就刚好k个)。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int N=1e5;
int main()
{
int n,k;
cin>>n>>k;
int f[N];
int c=;
for(int i=;i<=n;i++)
{
while(n%i==)
{
n/=i;
f[c++]=i;
}
}
if(c<k)cout<<-<<endl;
else
{
for(int i=;i<k-;i++)
cout<<f[i]<<' ';
int sum=;
for(int i=k-;i<c;i++)
sum*=f[i];
cout<<sum<<endl;
}
return ;
}
最新文章
- 跨平台开发之阿里Weex框架环境搭建(一)
- [IIS]IIS扫盲(五)
- Core Animation编程指南
- Table Groups [AX 2012]
- IIS线程池与ASP.NET线程池
- Java-->;分割文件
- UVA 10763 Foreign Exchange
- php设计模式之简单工厂模式
- Intel 被 ARM 逼急了
- C#管理IIS中的站点
- Servlet学习三:不允许直接访问jsp处理方式一过滤器
- [ERROR] Fatal error: Can&#39;t open and lock privilege tables: Table &#39;mysql.user&#39; doesn&#39;t exist 160913 02:11:21 mysqld_safe mysqld from pid file /tmp/mysql.pid ended
- swift pod第三方OC库使用use_frameworks!导致#import提示无法找到头文件
- luogu P5291 [十二省联考2019]希望
- jenkins+mail邮件配置
- spring的ioc与aop原理
- 【读书笔记】深入应用C++11代码优化与工业级应用 读书笔记01
- import、export使用介绍
- Ansible test
- C#winfrom最简DBHelp(数据库连接操作)