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 ;
}

Codeforces 797A - k-Factorization的更多相关文章

  1. Codeforces gym102152 K.Subarrays OR

    传送:http://codeforces.com/gym/102152/problem/K 题意:给定$n(n\le10^5)$个数$a_i(a_i\le10^9)$,对于任一个子数组中的数进行或操作 ...

  2. codeforces 1133E K Balanced Teams

    题目链接:http://codeforces.com/contest/1133/problem/E 题目大意: 在n个人中找到k个队伍.每个队伍必须满足最大值减最小值不超过5.求满足条件k个队伍人数的 ...

  3. Codeforces 1133E - K Balanced Teams - [DP]

    题目链接:https://codeforces.com/contest/1133/problem/C 题意: 给出 $n$ 个数,选取其中若干个数分别组成 $k$ 组,要求每组内最大值与最小值的差值不 ...

  4. codeforces 1269E K Integers (二分+树状数组)

    链接:https://codeforces.com/contest/1269/problem/E 题意:给一个序列P1,P2,P3,P4....Pi,每次可以交换两个相邻的元素,执行最小次数的交换移动 ...

  5. codeforces 1282B2. K for the Price of One (Hard Version) (dp)

    链接 https://codeforces.com/contest/1282/problem/B2 题意: 商店买东西,商店有n个物品,每个物品有自己的价格,商店有个优惠活动,当你买恰好k个东西时可以 ...

  6. Codeforces 544E K Balanced Teams (DP)

    题目: You are a coach at your local university. There are nn students under your supervision, the prog ...

  7. Codeforces Gym101502 K.Malek and Summer Semester

    K. Malek and Summer Semester   time limit per test 1.0 s memory limit per test 256 MB input standard ...

  8. Codeforces 987 K预处理BFS 3n,7n+1随机结论题/不动点逆序对 X&Y=0连边DFS求连通块数目

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  9. Codeforces Gym100971 K.Palindromization-回文串 (IX Samara Regional Intercollegiate Programming Contest Russia, Samara, March 13)

    这个题就是从字符串中删除一个字符,然后剩下的是回文串. 我写的代码虽然长得好看,但是循环里面的比较条件容易想错,太智障了... 一开始写的是计数比较,但是有的时候下标相同的也比较了,为了简单一些,直接 ...

随机推荐

  1. oracle 12cR1&12cR2核心高实用性新特性

    今天把本地环境升到了12.2.12c应该来说无论从性能和功能性方面都得到了很大的加强,就是不知道实际license怎么卖...原来有些功能在exadata中可用的,现在在12c中可用了,估计很大程度上 ...

  2. 10: VMware中扩展根分区

    1.1 添加一块硬盘 1.先给VMware添加一块60G硬盘 2.必须重启虚拟机才能识别到新加磁盘 fdisk -l        # 查看刚刚添加的硬盘 3.查看当前磁盘使用情况 df -hl   ...

  3. 实习日记a

    根据NPSaleAPI项目,学习了weUI 练习: <a href="javascript:;" class="weui_btn weui_btn_primary& ...

  4. Git clone、git reset

    一,git clone 1,git clone某一个分支 git clone -b <branch> <remote_repo> 2,.git 文件太大 :clone的时候,可 ...

  5. 查看Windows系统信息

    在终端输入"msinfo32". ms>Microsoft info>information

  6. Google Protocol Buffer在vs2010下配置

    1.从这里下载protobuf-2.6.1.tar.gz到桌面,并解压,解压后的文件夹为protobuf-2.6.1.(我的桌面为C:\Users\mcl\Desktop) 2 .进入文件夹proto ...

  7. minicom支持向串口自动发送命令的功能

    1. 用法 minicom -S <script name> -C <log name> 参数解析: -S: 指定要执行的脚本 -C: 指定输出日志文件名 2. 既然可以指定脚 ...

  8. git 如何revert指定范围内的commit并且只生成一个新的commit?

    答:一共分成两步 一. revert多个commit并生成多个新的commit git revert <old commit>^..<new commit> 二. 使用reba ...

  9. vba编程基础2

    安装office2010的时候, 最好是 完全安装/完整安装 , 这样可以查阅 excel的 "帮助文档" 帮助文档中包含了更多的/更详细的 参考信息. 普通模块无事件, 只有 子 ...

  10. 微生物增殖|2012年蓝桥杯B组题解析第一题-fishers

    (3')微生物增殖 假设有两种微生物 X 和 Y X出生后每隔3分钟分裂一次(数目加倍),Y出生后每隔2分钟分裂一次(数目加倍). 一个新出生的X,半分钟之后吃掉1个Y,并且,从此开始,每隔1分钟吃1 ...